Jon Hancock wrote:
> Here is a common situation. I have an attribute, :email_unconfirmed,
> it can be nil or a valid email address. I set up the following
> validations for it.
>
> validates_format :email_unconfirmed, :with => :email_address, :when =>
> [:new], :message => "Email has an invalid format"
> validates_present :email_unconfirmed, :when => [:new], :message =>
> "Email must be given"
>
> if I set a the :email_unconfirmed value to nil, I get double error
> messages:
> "Email has an invalid format Email must be given"
>
I thought you said nil is an acceptable value. Why are you validating
presence?
> Yep, merb displays them on one line since DM has them as two errors on
> the same attribute. It appears just like shown above." YuCK!!!
>
> This is one thing that I was afraid of when diving into using
> validations. They are a full on all-or-nothing loop. There are many
> cases where if something fails, you want to stop and just produce the
> first error. In this case, there should be a "dependent" validation;
> i.e. the email format is only checked if the value is not nil. This
> is obvious and should be a common case, but its not obvious how to
> code it.
>
If the desired behavior is what you described in your first paragraph
(:email_unconfirmed can be nil or a valid email address), then you can
do this:
validates_format :email_unconfirmed, :with => :email_address, :when =>
[:new], :message => "Email has an invalid format", :unless => Proc.new
{|obj| obj.email_unconfirmed.nil?}
hth,
Earle
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---