validation - Rails validate multiple attributes lambda only if all of them present -
i need validate few attributes in model when present in params
while updating or creating object.
validates :attribute_a,format: {with: /some_regex/}, :if => lambda{ |object| object.attribute_a.present? }
like attribute_a
there multiple attributes
may not present while being created
or updated
.instead of writing validates
statement each of them,is there way in can check presence of multiple attributes , validate every 1 common validation such inclusion_in
or format:{with:/some_regex/ }
.
i wanted following code wrong.
validates :attribute_a,attribute_b,attribute_c,attribute_d,:format => {:with => /some_regex/}, :if => lambda{ |object| object.attribute_name.present? }
you can use validates_format_of
:
validates_format_of :attr_a, :attr_b, with: /someregexp/, allow_blank: true
the allow blank option means regexp doesn't have match if attribute not present.
Comments
Post a Comment