8.3 Common Validation Options
The following options apply to all of the validation methods.
8.3.1 allow_blank and allow_nil
In some cases, you only want to trigger a validation if a value is present; in other words, the attribute is optional. There are two options that provide this functionality.
The :allow_blank option skips validation if the value is blank according to the blank? method. Similarly, the :allow_nil option skips the validation if the value of the attribute is nil; it only checks for nil, and empty strings "" are not considered nil, but they are considered blank.
8.3.2 if and unless
The :if and :unless options are covered in the next section, “Conditional Validation.”
8.3.3 message
As we’ve discussed earlier in the chapter, the way that the validation process registers failures is by adding items to the errors object of the model object being checked. Part of the error item is a specific message describing the validation failure. All of the validation methods accept a :message option so that you can override the default error message format.
class Account < ActiveRecord::Base validates_uniqueness_of :username, message: "is already taken" end
The default English locale file in Active Model defines most of the standard error message templates.
inclusion: "is not included in the list" exclusion: "is reserved" invalid: "is invalid" confirmation: "doesn't match %{attribute}" accepted: "must be accepted" empty: "can't be empty" blank: "can't be blank" present: "must be blank" too_long: "is too long (maximum is %{count} characters)" too_short: "is too short (minimum is %{count} characters)" wrong_length: "is the wrong length (should be %{count} characters)" not_a_number: "is not a number" not_an_integer: "must be an integer" greater_than: "must be greater than %{count}" greater_than_or_equal_to: "must be greater than or equal to %{count}" equal_to: "must be equal to %{count}" less_than: "must be less than %{count}" less_than_or_equal_to: "must be less than or equal to %{count}" other_than: "must be other than %{count}" odd: "must be odd" even: "must be even"
The default messages only use the count variable for interpolation, where appropriate, but model, attribute, and value are always available.
validates_uniqueness_of username, message: "%{value} is already registered"
8.3.4 on
By default, validations are run on save (both create and update operations). If you need to do so, you can limit a given validation to just one of those operations by passing the :on option either :create or :update.
Assuming that your application does not support changing emails, one good use for on: :create might be in conjunction with validates_uniqueness_of, since checking uniqueness with a query on large datasets can be time-consuming.
class Account < ActiveRecord::Base validates_uniqueness_of :email, on: :create end
8.3.5 strict
Rails 4 introduced a :strict validation option that defaults to false. Turning it on causes an exception ActiveModel::StrictValidationFailed to be raised when a model is invalid.
class Account < ActiveRecord::Base validates :email, presence: { strict: true } end
To override the type of exception raised on error, pass the custom exception to the :strict option.
class Account < ActiveRecord::Base validates :email, presence: { strict: EmailRequiredException } end