Validates_... :if option: only validate when not empty?

Hi all

I have several fields that are optional in a web form. E.g. the user may
enter his age or not.

Sadly the validates_numericality_of(:age) always fires, also when the
user omitted the field. So no entry results in a error message.
What’s the easiest way to fix this?

I tried

validates_numericality_of :age,
:if => Proc.new { |m| !m.age.empty? }

But somehow this just doesn’t seem to do anything?!

The coolest solution would be if I could only specify

:if => :not_empty?

Maybe there’s a plugin or something to achieve this behavior? :slight_smile:

I could write a method not_empty() myself:

def not_empty?
age.empty?
end

But what if I wanted to use the not_empty? method for other attributes,
too?

validates_numericality_of :age,
:if => :not_empty?

validates_numericality_of :height_in_inches,
:if => :not_empty?

Then I would need some way to determine which attribute is meant in the
not_empty? method. But I have no idea how to achieve this… :-/

So I guess I can’t just define a new method not_empty?() but have to
overwrite some existing code somehow, so it reacts special when
:not_empty? is delivered as :if option…? Please give me some help how
to do this or if there are even better solutions for this. :slight_smile:

Thanks a lot for help! :slight_smile:
Josh

On Oct 16, 4:55 pm, Joshua M. [email protected]
wrote:

validates_numericality_of :age,
:if => Proc.new { |m| !m.age.empty? }

But somehow this just doesn’t seem to do anything?!

This basic structure should work. When the value of the :if condition
is false, the validation will not run. So perhaps it’s the “empty?”
method – should it be “blank?” instead. I tried to find any
reference to the differences between blank? empty? nil?. Looking
further in the book, it seems that blank? is true if the receiver is
nil or an empty string. And there’s a thread I found on this group:
see
http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/12eca4adcddcea9d/b33248819234726f?lnk=gst&q=.empty#b33248819234726f

I had trouble finding “blank” documented in the API for ruby or rails,
but this could just be because I wasn’t looking in the right place :slight_smile:

Tom

Thank you!

It works now with the Proc. However, when doing the same with my own
validation method it does not work:

validates_url_format_of :url,
:if => Proc.new { |m| !m.url.empty? }

ActiveRecord::Errors::default_error_messages[:invalid_url_protocol] =
‘must be using the HTTP or HTTPS protocoll’
ActiveRecord::Errors::default_error_messages[:invalid_url_format] = ‘has
an incorrect link format’
module ActiveRecord::Validations::ClassMethods
def validates_url_format_of(*attr_names)
validates_each(attr_names.first.to_s) do |record, attr_name, value|
record.send("#{attr_name}=".to_sym, ‘http://’ +
record.send(attr_name.to_sym)) if !record.send(attr_name.to_sym).empty?
and record.send(attr_name.to_sym) !~ /^[a-z]+:///

  if record.send(attr_name.to_sym) !~ /^(http|https):\/\//i
    record.errors.add(attr_name, 

ActiveRecord::Errors::default_error_messages[:invalid_url_protocol])
elsif record.send(attr_name.to_sym) !~
/^(http|https)://([a-z0-9-äöüèà éê.]+)(.[a-z]{2,})(/(.*))?$/i
record.errors.add(attr_name,
ActiveRecord::Errors::default_error_messages[:invalid_url_format])
end
end
end

def validates_email_format_of(*attr_names)
# TODO: implement!
end
end

What do I have to implement that the :if option works?

(But anyway, the :if => :not_empty? would be cooler…)

hmm, just use the option

:allow_nil => true

Franz S. wrote:

hmm, just use the option

:allow_nil => true

Oh my god, didn’t see the forest because of all those trees! :wink: Thanks!

On Oct 16, 2007, at 9:00 PM, tharrison wrote:

user omitted the field. So no entry results in a error message.
This basic structure should work. When the value of the :if condition
but this could just be because I wasn’t looking in the right place :slight_smile:

Tom

Rails (ActiveSupport) defines blank? but doesn’t document it (the
code has #:nodoc: tags on the methods to keep rdoc from including it).

vendor/rails/activesupport/lib/active_support/core_ext/blank.rb

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs