Using :if with validates_xxx_of

I’m trying to make my validation conditional using the :if construct.

If I want to just test for the existence of the field EMAIL, it seems
like I could just do:

:if => ‘! EMAIL.nil?’

or do I have to use a proc?

And if I use a proc - what gets passed into the block - I assume the
currently validating field - is that right?

Thanks,
Wes

On 7/19/06, Wes G. [email protected] wrote:

And if I use a proc - what gets passed into the block - I assume the
http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Wes

YOu need to use either a proc or a method in the :if

I believe the obejct passed to the proc is the instance of the model
itself.
Something like

:if => Proc.new { |model| model.EMAIL.nil? }

Wes G. wrote:

I’m trying to make my validation conditional using the :if construct.

If I want to just test for the existence of the field EMAIL, it seems
like I could just do:

:if => ‘! EMAIL.nil?’

or do I have to use a proc?

And if I use a proc - what gets passed into the block - I assume the
currently validating field - is that right?

Thanks,
Wes

Got it working using :if => Proc.new { |job| (job.EMAIL != ‘’) } to only
validate when this field was submitted.

Of course, another trip to the source code. It was unclear from the
example whether the field itself or the AR object was passed into the
Proc block.

Also, I couldn’t get the string form to work - I believe because I don’t
understand how to pass a binding in.

Wes

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