# Validating multiple rows of data

**URL:** <https://www.ruby-forum.com/t/validating-multiple-rows-of-data/88721>\
**Category:** Rails\
**Created:** [March 13, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/validating-multiple-rows-of-data/88721 "2007-03-13T19:58:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [March 13, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/validating-multiple-rows-of-data/88721/1 "2007-03-13T19:58:07Z")

</div>

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?
