Today i came to point where nested validation could make my code more DRY. My question is, if anything like this planed? Example: Maybe you only want to display different error messages if an attribute is blank or has the wrong format. validates_presence_of :email_address, :message => ''Give me an address!'' validates_format_of :email_address, :with => /.+@.+\..{2,}/, :message => ''Something''s wrong with your address!'' This will give both messages if email_address is blank. The common way to get the second message only if email_address is not blank would be something like this: validates_presence_of :email_address, :message => ''Give me an address!'' validates_format_of :email_address, :with => /.+@.+\..{2,}/, :message => ''Something''s wrong with your email!'', :if => ''!record.email.blank?'' This could lead into multiple repetitions if you have more complex conditions or multi-level nested validations. Couldn''t a nicer and simpler syntax be: validates_presence_of(:email, :message => ''Give me an address!'') do validates_format_of :email, :with => /.+@.+\..{2,}/, :message => ''Something''s wrong with your email!'' end Maybe i missed something that already exists. Maybe this approach is not convertible. What do you think about this block syntax? -- Norman Timmler http://blog.inlet-media.de
On Jan 27, 2006, at 9:03 AM, Norman Timmler wrote:> Today i came to point where nested validation could make my code more > DRY. My question is, if anything like this planed? > > Example: > > Maybe you only want to display different error messages if an > attribute > is blank or has the wrong format. > > validates_presence_of :email_address, :message => ''Give me an > address!'' > validates_format_of :email_address, :with => /.+@.+\..{2,}/, > :message => ''Something''s wrong with your address!'' > > This will give both messages if email_address is blank. The common way > to get the second message only if email_address is not blank would be > something like this: > > validates_presence_of :email_address, > :message => ''Give me an address!'' > validates_format_of :email_address, > :with => /.+@.+\..{2,}/, > :message => ''Something''s wrong with your email!'', > :if => ''!record.email.blank?'' > > This could lead into multiple repetitions if you have more complex > conditions or multi-level nested validations. Couldn''t a nicer and > simpler syntax be: > > validates_presence_of(:email, :message => ''Give me an address!'') do > validates_format_of :email, :with => /.+@.+\..{2,}/, > :message => ''Something''s wrong with your email!'' > end > > Maybe i missed something that already exists. Maybe this approach > is not > convertible. What do you think about this block syntax? > > -- > Norman TimmlerWow, I really like that. I suspect it will be difficult to implement given the current validation code, but it would be a really sweet addition to the tools we''ve got. -- Duane
On 1/27/06, Duane Johnson <duane.johnson@gmail.com> wrote:> > On Jan 27, 2006, at 9:03 AM, Norman Timmler wrote: > > > Today i came to point where nested validation could make my code more > > DRY. My question is, if anything like this planed? > > > > Example: > > > > Maybe you only want to display different error messages if an > > attribute > > is blank or has the wrong format. > > > > validates_presence_of :email_address, :message => ''Give me an > > address!'' > > validates_format_of :email_address, :with => /.+@.+\..{2,}/, > > :message => ''Something''s wrong with your address!'' > > > > This will give both messages if email_address is blank. The common way > > to get the second message only if email_address is not blank would be > > something like this: > > > > validates_presence_of :email_address, > > :message => ''Give me an address!'' > > validates_format_of :email_address, > > :with => /.+@.+\..{2,}/, > > :message => ''Something''s wrong with your email!'', > > :if => ''!record.email.blank?'' > > > > This could lead into multiple repetitions if you have more complex > > conditions or multi-level nested validations. Couldn''t a nicer and > > simpler syntax be: > > > > validates_presence_of(:email, :message => ''Give me an address!'') do > > validates_format_of :email, :with => /.+@.+\..{2,}/, > > :message => ''Something''s wrong with your email!'' > > end > > > > Maybe i missed something that already exists. Maybe this approach > > is not > > convertible. What do you think about this block syntax? > > > > -- > > Norman Timmler > > Wow, I really like that. I suspect it will be difficult to implement > given the current validation code, but it would be a really sweet > addition to the tools we''ve got. > > -- Duane > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >Seconded! In fact, I think this would be an interesting project for me; unless someone else is working on this already (Norman?), I''d love to try my hand at it. Dave -- Dave Goodlad dgoodlad@gmail.com or dave@goodlad.ca http://david.goodlad.ca/
Am Freitag, den 27.01.2006, 08:21 -0800 schrieb David Goodlad:> Seconded! > > In fact, I think this would be an interesting project for me; unless > someone else is working on this already (Norman?), I''d love to try my > hand at it.It was just an idea. Go ahead Dave! -- Norman Timmler http://blog.inlet-media.de
Hi ! 2006/1/27, Norman Timmler <lists@inlet-media.de>:> validates_presence_of :email_address, :message => 'Give me an address!' > validates_format_of :email_address, :with => /.+@.+\..{2,}/, > :message => 'Something's wrong with your address!'In this specific case, couldn't you replace the regexp and leave of the validates_presence_of ? validates_format_of :email_address, :with => /\A.+@.+\..{2,}\Z/, :message => 'Something's wrong with your address, or it\'s missing!' Notice I added anchor points to the regexp. I do realize that this doesn't solve all cases, though. Bye ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Am Freitag, den 27.01.2006, 18:11 -0500 schrieb Francois Beausoleil:> 2006/1/27, Norman Timmler <lists@inlet-media.de>: > > validates_presence_of :email_address, :message => ''Give me an address!'' > > validates_format_of :email_address, :with => /.+@.+\..{2,}/, > > :message => ''Something''s wrong with your address!'' > > In this specific case, couldn''t you replace the regexp and leave of > the validates_presence_of ? > > validates_format_of :email_address, :with => /\A.+@.+\..{2,}\Z/, > :message => ''Something''s wrong with your address, > or it\''s missing!'' > > Notice I added anchor points to the regexp. I do realize that this > doesn''t solve all cases, though.This was more an example than a real world usage. Also the regular expression is not the one i usually use. Maybe it is a kind of philosophy, but is like concrete error messages for my clients and even like them if i have to complete a form. I am not a fan of ''Something is wrong here, but i don''t tell you what.'' The block syntax could help you to throw easy to understand and very specific error messages. -- Norman Timmler http://blog.inlet-media.de