So, here's a case something didn't work as expected, and I'm wondering
if a feature tweak is worth considering.
If I use :allow_nil => TRUE, validation rules still complain if a form
field is submitted empty (w/o using a presence_of rule).
So...
validates_format_of :first_name, :with => Is_human_name,
:allow_nil => TRUE, :message => Is_not_human_name_msg
Will complain if the field is submitted as empty. Now, empty doesn't
conform to the regex I am using, but I was expecting that :allow_nil
will cause the validation to be ignored when the value is empty.
After thinking about it, I can see that params creates emptry strings
which are not interpreted as nil for saving. That makes sense, and
therefore I can see why Rails still validated my format rule.
Ultimately what I was trying to do was have certain messages override
the need for others.
If I have this:
validates_presence_of :first_name
validates_format_of :first_name, :with => Is_human_name,
:allow_nil => TRUE, :message => Is_not_human_name_msg
And the field is submitted as empty, then the only error message I need
to see is the one that says the field cannot be empty. I do not need to
see the format_of error message, or another any subsequent rule failure
that might apply.
It was suggested that using an :if is the typical strategy for
accomplishing that:
:if => Proc.new { |model_name| model_name.first_name.length > 0 }
That seems rather bulky (especially when repetitive), and I wonder if an
:allow_empty => TRUE couldn't be used to provide the behavior I was
expecting -- rules are ignored for *empty* fields, not just Nil ones.
Or perhaps it is better named, :ignore_empty => TRUE
Maybe this is too small a use case, but it sure reduces the :if bulk.
Just a thought...
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" 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/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---