Forum: Ruby on Rails Validating multiple rows of data

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
17a97e09a1b23bc733ae22880a961b59?d=identicon&s=25 Kevin (Guest)
on 2007-03-13 19:58
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

Here's the behavior I'd like to see:

1. 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

2. 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 =>{ |person|
! }

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?
This topic is locked and can not be replied to.