Rails: Iterate dynamically over params hash to verify they exist -
i've got form quite bit of params being passed controller processing. different 'sets' of params named in similar fashion:
setname1_paramname setname1_paramname2
now, need check 1 of these 'sets' verify of fields submitted. right now, i'm doing manual if or style statement:
if setname1_paramname.blank? || setname1_paramname2.blank? || ...etc @object.errors.add_to_base("all setname1 fields required."). render :action => 'new' return false end
is there way programmatically loop on these params, , add them @object errors?
thanks!
since sounds have ton of params , seems need able checks on groups of params, maybe useful? basically, iterate on params hash, , use regular expressions target sets of params. then, inside loop, can sort of validations:
params.each |key, value| # target groups using regular expressions if (key.to_s[/setname1.*/]) # whatever logic need params start 'setname1' if param[key].blank? @object.errors.add_to_base("all setname1 fields required."). end end end
Comments
Post a Comment