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 => /[EMAIL PROTECTED],}/,
                    :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 => /[EMAIL PROTECTED],}/,
                    :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 => /[EMAIL PROTECTED],}/,
                      :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

_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to