I need "validates_presence_of" help

Hi - I have 3 fileds a user can fill out. They can fill out 2 of them or
just one of them, for example.

fill out these 2 fields:
Field1 and Field2

Or fill out this field:
Field3

In my model how do I use validates_presence_of for Field1 and Field2 or
just Field3.

I want to do something like this:
validates_presence_of Field1, Field2
OR
validates_presence_of Field3

So the user can fill out Field1, Field2 and be a succesful update or
just fill out Field3 and be a succesful update. Any help will be
apreciated, thank you.

validates_presence_of :field1, :message => “yo, fix it 1.”, :if =>
:first_set?
validates_presence_of :field2, :message => “yo, fix it 2.”, :if =>
:first_set?
validates_presence_of :field3, :message => “yo, fix it 3.”, :if =>
:second_set?

def first_set?
r = true
if (field1 && field2)
r = false
end
return r
end

def second_set?
#etc etc
end

Dunno if that works, but the :if part is what you’re looking for. needs
to return true or false.

The API docs might help
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000813

something like this should work

validates_presence_of Field1, Field2 :if => Proc.new {|model|
model.Field3.nil? }
validates_presence_of Field3 :if => Proc.new {|model| model.
Field1.nil? and model.Field2.nil? }

Seb
Le 10 août 06 à 21:37, Ras R. a écrit :

On 8/10/06, Ras R. [email protected] wrote:

just Field3.

I want to do something like this:
validates_presence_of Field1, Field2
OR
validates_presence_of Field3

I think you are going to have to write a custom validation routine in
your model. There are several ways to do that. validates_presence_of
works off #blank? for the field. So you logic could be as simple as:

def validate
def validate
if field3.blank?
errors.add_on_empty([:field1, :field2], “You must enter
field 1 and field 2 or just field3”)
end
end
end

Ed

Sorry, extra def valid there:

def validate
    if field3.blank?
        errors.add_on_empty([:field1, :field2], "You must enter

field 1 and field 2 or just field3")
end
end

Ed