Forum: Ruby on Rails validates_format_of nil

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.
List R. (Guest)
on 2005-11-28 18:04
I've noticed that validates_format_of will return invalid if the field
is nil.

Question 1) How should we declare a validation that will validate it
with a regex if it is set, but, if the field is not set, just move on

Question 2) Why is it done this way?  The other validators are only
invoked if the field is set .  If you want to declare the field
mandatory, you add a validates_presence_of.  This seems to me to be the
best design - why does validates_format_of break this?
List R. (Guest)
on 2005-11-28 20:13
Am I making a mistake here?  If so, someone please clue me in.

Likewise, if my questions are unclear, please let me know, and I'll try
to clarify.

lstrecv wrote:
> I've noticed that validates_format_of will return invalid if the field
> is nil.
>
> Question 1) How should we declare a validation that will validate it
> with a regex if it is set, but, if the field is not set, just move on
>
> Question 2) Why is it done this way?  The other validators are only
> invoked if the field is set .  If you want to declare the field
> mandatory, you add a validates_presence_of.  This seems to me to be the
> best design - why does validates_format_of break this?
shawn_garbett (Guest)
on 2005-11-29 00:31
(Received via mailing list)
--- List R. <removed_email_address@domain.invalid> wrote:
> > is nil.
> > mandatory, you add a validates_presence_of.  This
> seems to me to be the
> > best design - why does validates_format_of break
> this?
>

I argued elsewhere that the only validation that
should fail on nil is the validates_presence_of. That
makes this concept orthogonal to all other
validations. Results in less code most of the time to.


The argument was not accepted, so we've branched our
rails tree, and it will have this behavior. I'll be
posting a big patch somewhere soon.

It was mentioned that this will be changed in a post
1.0 release.

Shawn



__________________________________________
Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
List R. (Guest)
on 2005-11-29 03:55
Couldn't have said it better myself.

Note that some of the validations (ie, validates_association) *do*
follow this behavior.

Shawn, could you post a link to the discussion you mentioned, as well as
to the announcement that this will be added?

Also, in the meanwhile, what is the preferred idiom for simulating this
(ie, validates_format_of when not nil)?


shawn_garbett wrote:
> --- List R. <removed_email_address@domain.invalid> wrote:
>> > is nil.
>> > mandatory, you add a validates_presence_of.  This
>> seems to me to be the
>> > best design - why does validates_format_of break
>> this?
>>
>
> I argued elsewhere that the only validation that
> should fail on nil is the validates_presence_of. That
> makes this concept orthogonal to all other
> validations. Results in less code most of the time to.
>
>
> The argument was not accepted, so we've branched our
> rails tree, and it will have this behavior. I'll be
> posting a big patch somewhere soon.
>
> It was mentioned that this will be changed in a post
> 1.0 release.
>
> Shawn
>
>
>
> __________________________________________
> Yahoo! DSL � Something to write home about.
> Just $16.99/mo. or less.
> dsl.yahoo.com
shawn_garbett (Guest)
on 2005-11-29 16:57
(Received via mailing list)
--- List R. <removed_email_address@domain.invalid> wrote:
> Couldn't have said it better myself.
>
> Note that some of the validations (ie,
> validates_association) *do*
> follow this behavior.

Some will logically require nil be an error, but in
general I think nil is an orthogonal concern.

>
> Shawn, could you post a link to the discussion you
> mentioned, as well as
> to the announcement that this will be added?
>
> Also, in the meanwhile, what is the preferred idiom
> for simulating this
> (ie, validates_format_of when not nil)?

http://dev.rubyonrails.org//ticket/928/

I'm going to start next week preparing the patch for
submission.

Shawn G.




__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
This topic is locked and can not be replied to.