Hello,
I have problem with custom validator which should check such
situation:
I have two fields text_field ‘object’, ‘others’ and text_field
‘object’, ‘others_area’
I “raise” error if others is nil but others_area is filled up also
when others is not nil but others_area isn’t numeric
If both fields are nil it’s ok i allow such situation.
How can I do that in one correlated validator?
Best regards,
Adr
Please attach some code or try explain better, because i don’t
understand you
On Nov 14, 2:30 pm, jprogramista [email protected] wrote:
Best regards,
Adr
Simple. Use custom validation in your model of ‘object’.
Use something like this:
class Bla < ActiveRecord::Base
def validate
if others.nil? and others_area.not_numeric? then
errors.add(“nasty thing”, “happenned”)
end
end
end
you need to write your own validation with validates_each
something like:
validates_each :others, :others_area, :on => :create, do |record, attr,
value|
unless others then #check for nil
… # do your validation here
# and add error to record if necessary:
record.errors.add :others, ‘your message’
end
end
end
On Nov 14, 10:43 am, Thorsten M. <rails-mailing-l…@andreas-
s.net> wrote:
you need to write your own validation with validates_each
something like:
validates_each :others, :others_area, :on => :create, do |record, attr,
Hi thx for response, i tried to write way you described but stuck with
such code:
def validates_correlated_presence_of(*attr_names)
send(validation_method(:create)) do |record|
value1 = record.send(attr_names[0])
value2 = record.send(attr_names[1])
if(!value1.nil? || !value2.nil?)
#validates_presence_of(attr_names[0], attr_names[1], :message =>
“must be filled”)
validates_presence_of(attr_names[0], :message => “must be
filled”)
validates_presence_of(attr_names[1], :message => “must be
filled”)
end
end
end
Validator goes all lines of code but
validates_presence_of(attr_names[0], :message => “must be filled”)
isn’t adding any errors to record object. Is it possible to embed one
validator into another?
Best regards,
Adr