Active Record gives many predefined validation helpers which you can use directly in your class. These helpers provide us rules which can be commonly used as a validation rules. So if the validation fails, a error message is added to object error collection and that particular message is associated with the attribute that is going to be validated.
So except :on and :message options, which define when the validation should be run and what the message will be added to the errors collection where :on option will take one of the values i.e. :create or :update and on the other hand for the error message there is a default error message for each and every validation helpers we have in the rails. and the default message would be taken only in case where it is not specified. otherwise it will take the one we specified as :message. So let's take a look of the available helpers we have:
Acceptance
Its most common use is seen when the users agreement is required to your application's terms of service, which comes after a concept like confirm reading some text. It could be rightly said that this validation is strictly related to web applications and there is no need for this 'acceptance' to be documented anywhere in your database. In case of non availability of DB field for that, a virtual attribute can be easily created by the helper for you.
class User < ActiveRecord::Base
validates :terms_of_service, acceptance: true
end
As far as default error message for this helper is concerned, it is "must be accepted".
It can receive an :accept option, which regulates the value that will consider acceptance. It defaults to "1" and can be easily changed.
class User < ActiveRecord::Base
validates :terms_of_service, acceptance: { accept: 'yes' }
end
validates_associated
This helper can be used when there exists associations between your model and other models. Please note that they also need to be validated. At the time you save your object, valid? is called upon each one of the associated objects.
class User < ActiveRecord::Base
has_many :profiles
validates_associated :profiles
end
This validation is fit for all of the association types.
Note: Please refrain from using validates_associated on both ends of your associations. An infinite loop makes then stuck after calling them.
"Is invalid" is the default error message for validates_associated. Each associated object will contain its own errors collection; errors do not drop up to the calling model.
confirmation:
This particular could be brought into use where you have two text fields that should receive exactly the same content. For example, if you want to confirm password or email address. With the help of this validation, it becomes easier to create a virtual attribute. Its name goes by the name of the field and the confirmation has to be done with _confirmation" appended at the last.
class User < ActiveRecord::Base
validates :password, confirmation: true
end
In your view template you could use something like
<%= text_field :person, :password %>
<%= text_field :person, :password_confirmation %>
This check will be performed only if password_confirmation is not nil. To require this confirmation, make sure to add a presence check for the confirmation attribute:
class User < ActiveRecord::Base
validates :password, confirmation: true
validates :password_confirmation, presence: true
end
The default error message for this particular helper is "doesn't match confirmation". We can change that if we want.
exclusion:
In this helper it basically validates the attributes values which are not included in a given set. Even this set can be any enumerable object.
class Keyword < ActiveRecord::Base
validates :reserved_keywords, exclusion: { in: %w(class object),
message: "%{value} is reserved keywords." }
end
The exclusion helper has a :in options which means the set will not be accepted from the attributes values. :in option has an alias called :within that you can use for the same purpose, if you'd like to. This example uses the :message option to show how you can include the attribute's value.
The default error message is "is reserved".
0 Comment(s)