Form Validation

Hi,

I’ve got an interesting one here :slight_smile:

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?

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