Validates_format_of nil

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?

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?

— List R. [email protected] 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

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. [email protected] 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. [email protected] 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