Hello everyone,
As we know in almost all the web applications there are forms, text boxes and several other fields which need big time validations so that the security of the application is maintained and no one should be able to alter it or play around with it. Today we will go through some common server-side validations in rails which are provided by
active records. These validations are predefined in an active records and we can easily use them for server side validations.
This type of validation is used to see if a checkbox is checked or not, for example accepting terms and conditions etc. We don't need to create a special column in our user table to see if the checkbox is checked or not. It's created virtually.
class User < ActiveRecord::Base
validates :terms_of_service, acceptance: true
end
This validation is used to check if the user has filled the specific field or not. For example, there is a registration form in which user has to fill his details. Now for an instance there is an address field which has to be compulsorily filled then we can use this validation in our user model.
class User < ActiveRecord::Base
validates :address, presence: true
end
We use this validation if we have two fields which need exactly the same text to be filled in both of them. Like, for example, we have a email field which needs the email to be confirmed two times so for that we use this validation.
class User < ActiveRecord::Base
validates :email, confirmation: true
validates :email_confirmation, presence: true
end
So these were some of the most common server side validations which active records provide us.
0 Comment(s)