I have a form that allows users to enter up to 5 names and phone numbers
at once. However, I’m having a problem getting the validation to work
properly.
Here’s the behavior I’d like to see:
-
If a name is entered for a row, Rails should validate both the name
and phone number to verify its presence and assure it’s properly
formated. -
If no data is entered for a row, Rails should ignore it by not
validating it or adding it to the database.
One way I figured out how to get the first requirement working is by
adding an if statement to my validation helpers. For example:
validates_presence_of :name, :phone, :if => Proc.new{ |person|
!person.name.blank? }
This will only perform validation on a row if the name is filled in. The
problem is, if only 3 out of the 5 rows on the form are completed, Rails
will add the blank rows to the database. Is there a way to prevent this
while still allowing users to enter less than 5 names at a time?