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. -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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 On 8/10/06, Ed Howland <ed.howland@gmail.com> wrote:> On 8/10/06, Ras Ras <ra56179n@myway.com> wrote: > > 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 > > > > 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 > > 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. > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Ed Howland > http://greenprogrammer.blogspot.com >-- Ed Howland http://greenprogrammer.blogspot.com
On 8/10/06, Ras Ras <ra56179n@myway.com> wrote:> 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 >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> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ed Howland http://greenprogrammer.blogspot.com
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 Ras a ?crit :> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060811/9973cbd9/attachment.html