Hi there,
I''ve got the following in my model:
validates_format_of :postcode,
:with => /^([a-zA-Z]{1,2}[0-9]{1,2} [0-9]{1}[a-zA-Z]{2})$/,
:message => "is not valid",
:if => :postcode
... but it''s not working - it always insists on validation, even if
postcode
is empty. Basically, the postcode field is optional, but if it''s
filled in, I
want to check the format of it. However, the above does work if I use :if
with some other field, just not the field being validated! So, if I make it
dependent on some other field having a value (empty = do not validate, value =
validate), it works fine, just not with itself as the field in the :if clause.
I''ve been asking in #rubyonrails, but nobody seems to know. I guess I
could
build the "or blank" condition into my regular expression, but
I''m certain
that can''t be the best way to do it? Surely this should be an easy
thing to
do? Perhaps someone could help?
Many thanks in advance,
~Dave
--
Dave Silvester
Rent-A-Monkey Website Development
Web: http://www.rentamonkey.com/
There''s probably a way to do it, but until someone tells you how, just put it in the validate method. First check to see if it''s nil/blank, and if it''s not, go ahead and do the validation. Pat On 8/10/05, Dave Silvester <dave-AJqNGCqIqVQ7cdpDWioORw@public.gmane.org> wrote:> Hi there, > > I''ve got the following in my model: > > validates_format_of :postcode, > :with => /^([a-zA-Z]{1,2}[0-9]{1,2} [0-9]{1}[a-zA-Z]{2})$/, > :message => "is not valid", > :if => :postcode > > ... but it''s not working - it always insists on validation, even if postcode > is empty. Basically, the postcode field is optional, but if it''s filled in, I > want to check the format of it. However, the above does work if I use :if > with some other field, just not the field being validated! So, if I make it > dependent on some other field having a value (empty = do not validate, value > validate), it works fine, just not with itself as the field in the :if clause. > > I''ve been asking in #rubyonrails, but nobody seems to know. I guess I could > build the "or blank" condition into my regular expression, but I''m certain > that can''t be the best way to do it? Surely this should be an easy thing to > do? Perhaps someone could help? > > Many thanks in advance, > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Here''s how I do it. Hopefully someone can show me an even better way,
though.
validates_format_of :postcode,
:with => /<your pattern>/,
:if => Proc.new { |model| !model.postcode.blank? }
Dave Silvester wrote:> Hi there,
>
> I''ve got the following in my model:
>
> validates_format_of :postcode,
> :with => /^([a-zA-Z]{1,2}[0-9]{1,2} [0-9]{1}[a-zA-Z]{2})$/,
> :message => "is not valid",
> :if => :postcode
>
> ... but it''s not working - it always insists on validation, even
if
> postcode is empty. Basically, the postcode field is optional, but if
> it''s filled in, I want to check the format of it. However, the
above
> does work if I use :if with some other field, just not the field being
> validated! So, if I make it dependent on some other field having a
> value (empty = do not validate, value = validate), it works fine, just
> not with itself as the field in the :if clause.
>
> I''ve been asking in #rubyonrails, but nobody seems to know. I
guess I
> could build the "or blank" condition into my regular expression,
but I''m
> certain that can''t be the best way to do it? Surely this should
be an
> easy thing to do? Perhaps someone could help?
>
> Many thanks in advance,
>
> ~Dave
>
Dave Silvester
2005-Aug-10 19:47 UTC
Re: validates_format_of on an optional field using :if
Wilson wrote:> :if => Proc.new { |model| !model.postcode.blank? }Thanks, that works great! I''d already tried something a little like that using nil, and again using defined, but couldn''t get it to work. Hadn''t encountered blank until now, so that''s very useful! This still seems a little bit hack-ish to me though, as surely one should just be able to use: :if => :the_current_field_name ... because that works great using... :if => :some_other_field I wonder if this is a deliberate thing or not? ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
I think basically they didn''t want to override the behavior of
"nil?" and/or
"empty?" for all Ruby objects, because that could lead to unexpected
mayhem.
To work around that, "blank?" is added to the base Object class, and
returns
true if something is 0, nil, "", /\s+/, etc.
You''re right, though, I think that the validator methods should be
changed to
let you use this very common idiom without the "Proc" syntax, which
will be
unfamiliar to new Ruby users.
For grins, I just took a look at the validation code, and found something
interesting. Here''s the method that actually evaluates the
":if"
def evaluate_condition(condition, record)
case condition
when Symbol: record.send(condition)
when String: eval(condition, binding)
else
if condition_block?(condition)
condition.call(record)
else
raise(
ActiveRecordError,
"Validations need to be either a symbol, string (to be
eval''ed), proc/method, or " +
"class implementing a static validation method"
)
end
end
end
I haven''t tried it, but it looks like you could also do:
:if => "!postcode.blank?"
However, I haven''t tried that, and I''ve never really done much
with eval(), so I
might be missing something, or it might need to be
"!self[:postcode].blank?" or
something funny.
--Wilson.
Dave Silvester wrote:> Wilson wrote:
>
>> :if => Proc.new { |model| !model.postcode.blank? }
>
>
> Thanks, that works great! I''d already tried something a little
like
> that using nil, and again using defined, but couldn''t get it to
work.
> Hadn''t encountered blank until now, so that''s very
useful!
>
> This still seems a little bit hack-ish to me though, as surely one
> should just be able to use:
>
> :if => :the_current_field_name
>
> ... because that works great using...
>
> :if => :some_other_field
>
> I wonder if this is a deliberate thing or not?
>
> ~Dave
>
Dave Silvester
2005-Aug-10 20:24 UTC
Re: validates_format_of on an optional field using :if
Wilson wrote:> I haven''t tried it, but it looks like you could also do: > :if => "!postcode.blank?" > However, I haven''t tried that, and I''ve never really done much with > eval(), so I might be missing something, or it might need to be > "!self[:postcode].blank?" or something funny.Yeah, I''d already tried something very similar to your first example above (albeit using nil instead), but none of them seem to work. I think the best way so far seems to be the: :if => Proc.new {|model| !model.postcode.blank?} Thanks very much for your help though, consider my problem solved! :-) ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/