I am trying to validate a form, for that I have to validate three inputs that would be the lot, the quantity of packaging and the quantity of boxes, if the three are correct, they are entered into the database, how can I validate one by a? and if any have an error return, I was thinking of a function and do a for loop from 0 to 3 since there are only three variables and if there is an error it exits the loop, but is there a more elegant way?
It should be noted that I have two regular expressions, one for quantity and boxes and the other for lot
Probably making a
if
with three conditions is the most logical solution in your case, but if you have several forms it could be useful to parameterize the validation. The validation rules could come from the server (where you have to validate anyway) thereby ultimately avoiding validation errors and code duplication.By making the necessary changes to
validate_input()
, in the hashvalidation_rules
you could have regular expressions in the fieldname
, define a validation function instead of (or in addition to) a regular expression, etc.But as I say, in your case of a form with 3 values, without more context, I would use a
if
and that's it.