Validating multiple rows of data

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:

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

  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 => 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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs