Validates_format_of / nil

When using validates_format_of I am not able to enter a nil value?

It’s a mute point that I think that validates_format_of should NOT do
the job of validates_presence_of, but how should i get around this?

I want to validate a field only if someone has entered something in,
otherwise i do not want it to validate nothing. I do not mind if he user
enters nothing, but if they intend for something to be there, it needs
the correct format.

HELP!?

thanks!
mk

Validations provide mechanisms to do what you’re asking. See the API on
validations for more info.

Possible solutions can be found via google “validates_format_of nil”

http://significantbits.wordpress.com/2007/03/02/apply-rails-validation-rules-only-when-the-attribute-is-not-nil/

http://brighter.net/2007/6/23/validates_format_of-something-without-validates_presence_of

Good luck!

validates_format_of :foo, :with => /bar/, :if => Proc.new(|o| o.foo !=
nil }

Mason K. wrote:

HELP!?

thanks!
mk


Sincerely,

William P.

:allow_nil works for most of the validates_ calls, but it is not an
option of validates_format_of

Mason K. wrote:

Thanks!


Sincerely,

William P.

Thank you!

:allow_nil => true

did not work (though some said it worked fine for them)

:if => column_name?

did work though.

Thanks!

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