Can I use validates_presence_of somehow if I want to check that at least
one of 2 fields is not empty?
Thanks,
Dmitry
Can I use validates_presence_of somehow if I want to check that at least
one of 2 fields is not empty?
Thanks,
Dmitry
I’m still new to Rails, but I think you need to add a ‘validate’ method
to your controller and perform the check in there instead.
Off the top of my head, something like:
protected
def validate
errors.add(“Either tweedledum or tweedledee need to be entered.”)
unless tweedledum != nil or tweedledee != nil
end
I can’t see a way of using validates_presence_of for this.
Regards,
Chris.
I think Chris meant to tell you to add the validate method to the model.
This will then be called in the normal validation routine.
Mark
On 9/7/06, Dmitry H. [email protected] wrote:
errors.add(“Either tweedledum or tweedledee need to be entered.”)
And is it possible to embed this ‘validate’ method into the model; say,
into the before_save filter or something like that?–
Posted via http://www.ruby-forum.com/.
–
Mark Van H.
[email protected]
http://lotswholetime.com
Chris D. wrote:
I’m still new to Rails, but I think you need to add a ‘validate’ method
to your controller and perform the check in there instead.Off the top of my head, something like:
protected
def validate
errors.add(“Either tweedledum or tweedledee need to be entered.”)
unless tweedledum != nil or tweedledee != nil
endI can’t see a way of using validates_presence_of for this.
Regards,
Chris.
Thanks, Chris!
And is it possible to embed this ‘validate’ method into the model; say,
into the before_save filter or something like that?
Thanks, Chris!
And is it possible to embed this ‘validate’ method into the model; say,
into the before_save filter or something like that?
If you have a method called “validate” on your model, this is used
automatically by the valid? method. No need to do anything else.
Max
Hi !
2006/9/7, Max M. [email protected]:
If you have a method called “validate” on your model, this is used
automatically by the valid? method. No need to do anything else.
The only problem with that is if you have subclasses that override the
validate method. Unless the subclass calls the superclass method,
your validations won’t work as expected.
class Order < AR::Base
def validate
# this will never be called
end
end
class RushOrder < Order
def validate
self.errors.add(:status, ‘invalid’) if self.status.blank?
end
end
If you do this instead:
class Order < AR::Base
before_validation :check_base_order
def check_base_order
# this will always be called
end
end
class RushOrder < Order
before_validation :check_rush_order
def check_rush_order
# this will ALSO always be called
end
end
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/
Yes, I meant model, but wrote controller. Also it should be
@tweedledum etc. The ‘unless’ is a continuation of the ‘errors.add’
line, but it’s come out with bad formatting.
Regards,
Chris.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs