If you need to code a considerably complex validation, the error sometimes doesnt lie in a particular attribute, but in a combination of several of them.
For example, if i want to validate that a the time period between :start_date and :end_date doesnt contain any sunday, the error doesnt belong specifically to either of those fields, but the Errors add method requires to specify it.
A very common way to skip validation is by keeping the value for immediate attribute as 'true' for the UIComponents. Immediate attribute allow processing of components to move up to the Apply Request Values phase of the lifecycle. scenario: While canceling a specific action, system should not perform the validation.
So remember folks, validates is for Rails validators (and custom validator classes ending with Validator if that's what you're into), and validate is for your custom validator methods.
1 What is Active Record? Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
Rails built-in Validation MethodsValidates whether associated objects are all valid themselves. Work with any kind of association. It validates whether a user has entered matching information like password or email in second entry field. Validates each attribute against a block.
Try doing something like this:
# Your Model.rb validate :my_own_validation_method ... private def my_own_validation_method if there_is_no_sunday_in_the_range self.errors[:base] << "You must have a Sunday in the time range!" end end
Basically, you can add your own complex validations to a model, and when you see that something erroneous has happened, you can add an error string in the array of errors.
model_instance.errors[:base] << "msg"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With