Forum: Ruby on Rails Form Validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef31136ad29b9ce06c9f718109a584cc?d=identicon&s=25 Pete (Guest)
on 2006-03-21 18:11
Hi,

I've got an interesting one here :)

I have a form with three fields :

serial_number
initial
surname


now, if serial_number is entered, it must be between 1 and 64000. If
it's filled in, the other two fields must be blank. If serial_number is
left blank then the other two boxes must be filled.


Any suggestions?
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2006-03-21 19:31
(Received via mailing list)
do a custom validation in your model

 def validate
   if serial_number.nil?
     errors.add('field1', "Sorry field1 cant be blank when using a
serial
number")  if field1.nil?
     errors.add('field2', "Sorry field2 cant be blank when using a
serial
number")  if field2.nil?
   else
     errors.add('serial_number',"Sorry serial number must be between 1
and
64000") if serial_number < 1 || serial_number > 64000
     errors.add('field1', "Sorry field1 must be blank when using a
serial
number")  if ! field1.nil?
     errors.add('field2', "Sorry field2 must be blank when using a
serial
number")  if ! field2.nil?
   end
 end

     quick and dirty but should come close :-)   Sometimes you have to
use
field.nan? instead of nil? if the value is a number, at least in my
experiences. Not sure why types of data is in your fields.

adam
This topic is locked and can not be replied to.