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? -- Posted via http://www.ruby-forum.com/.
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?-- Posted via http://www.ruby-forum.com/.
--- List Recv <listrecv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? >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 Recv <listrecv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-- Posted via http://www.ruby-forum.com/.
--- List Recv <listrecv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 Garbett __________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com