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_inor 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

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -