Forum: Ruby on Rails Either/Or 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.
602d5e1712946ae075e1c687e1318483?d=identicon&s=25 Joshua Schairbaum (Guest)
on 2006-01-30 22:20
How would I use validations to ensure I either received a blank value or
a ten digit number?

If I set validates_length_of :field, :in => 0..10 then that would allow
any value in-between.  The allow_nil => true option does not seem to
work.

I've also tried using this with some combination of
validates_numericality_of but cannot get it to work.

Any help would be greatly appreciated.

Thanks,
Josh
17cc8c944c49362c183fe253aa6c3e05?d=identicon&s=25 Dave Teare (Guest)
on 2006-01-31 00:03
(Received via mailing list)
There might be a some funky validates_* methods I don't know of, but
typically I define my own validate method whenever I need to do anything
more than basic validations.

To do this, just add a validate method to your AR class; here's a
snippet from my site:

def validate
  is_taken = Member.find_by_email(self.email)

  unless (is_taken.nil? || is_taken.id == self.id)
    errors.add_to_base("The email address <b>#{self.email}</b> has
already been registered on DevLists.")
  end
end

HTH.

On Monday, January 30, 2006, at 10:20 PM, Joshua Schairbaum wrote:
>Any help would be greatly appreciated.
>
>Thanks,
>Josh
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


Cheers!
--Dave.
This topic is locked and can not be replied to.