Validation: Excluding Regexps

Go To StackoverFlow.com

0

How can a validation exclude a regexp?

I'm looking for something like this:

validates_format_of :string, :without => /\A(something)\Z/

which is mentioned here, but I don't think is a real thing (Unknown validator: 'WithoutValidator).

2012-04-05 21:53
by mahemoff
which version of rails are you using - Sairam 2012-04-05 21:55
@Sairam 3.1 (added the tag - mahemoff 2012-04-05 22:06
The :without option has been there since August of 2009. Are you calling validates_format_of exactly like in your example - sluukkonen 2012-04-05 22:30
Actually no, so that may be it. I was calling validates :format =>. I thought they were equivalent and perhaps the validates* methods deprecated, as the official guide doesn't mention them? http://guides.rubyonrails.org/activerecordvalidationscallbacks.htm - mahemoff 2012-04-05 23:10


-1

You could write your own:

validate :name_exclude_pattern

def name_exclude_pattern
  unless self.name !~ /\A(something)\Z/
    errors.add(:name, "invalid format.")
  end
end

Though of course, this would have to be done for each attribute you want to check.

2012-04-05 22:08
by tsherif
Ads