Answer the question
In order to leave comments, you need to log in
How to make validation scripts in the model
There is a user registration and his personal account.
I need to validate fields during registration: email, password, repeat password.
And there is a personal account in which you need to validate other fields: name, phone. And you can't change your email.
And another form in the office, in which you can only change the password.
In the Yii world, this was called scripting. In the model for the validation of attributes, scenarios were set under which they worked. Then the desired scenario was set for the model instance and due to this, the correct validation took place.
Is there something similar in RoR?
Answer the question
In order to leave comments, you need to log in
I think that in this case it is more convenient to use with_option.
At the beginning, general validations for all form fields (if any) are described, and then conditional ones.
validates :customertype, :presence => true, :inclusion => { :in => ["person","company"] }
with_options :if => :is_person? do |person|
person.validates :email, :presence => true, :length => {:minimum => 5}, :format => {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i}
end
def is_person?
self.customertype == "person"
end
Finally, the options :if, :unless, :on
validate :must_be_friends, :on => :create, :if => Proc.new {|comment| some_condition}
apidock.com/rails/v3.2.8/ActiveModel/Validations/ClassMethods/validate
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question