I have a user signup form that has the usual fields (name, email, password, etc...) and also a "team_invite_code" field and a "role" popup menu.
Before creating the user - only in case the user role is "child" - I would need to:
How can I write a proper validation in Rails 2.3.6 ?
I tried the following, but it is giving me errors:
validate :child_and_team_code_exists
def child_and_team_code_exists
errors.add(:team_code, t("user_form.team_code_not_present")) unless
self.is_child? && Team.scoped_by_code("params[:team_code]").exists?
end
>> NameError: undefined local variable or method `child_and_team_code_exists' for #<Class:0x102ca7fa8>
UPDATE: This validation code works:
def validate
errors.add_to_base(t("user_form.team_code_not_present")) if (self.is_child? && !Team.scoped_by_code("params[:team_code]").exists?)
end
Your validate method child_and_team_code_exists
should be a private or protected method, otherwise in your case it becomes an instance method
validate :child_and_team_code_exists
private
def child_and_team_code_exists
errors.add(:team_code, t("user_form.team_code_not_present")) unless
self.is_child? && Team.scoped_by_code("params[:team_code]").exists?
end
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