Validates_presence_of

I am just going through the book and I come across a problem with the
code in the app/models:

validates_format_of :image_url,
    :with => %r{^http:.+\.(gif|jpg|png)$}i,
    :message => "must be a URL for a GIF, JPG, or PNG image"

It seems to moan when it’s empty even though I removed :image_url from
validates_presence_of. How should I make it optional? Altering the
regex?

Btw I have the same sort of problem with example code for the price
example. I want it optional unless I used validates_presence_of. Isn’t
that sensible?

In the SQL DDL, does NOT NULL mean anything here?

On 2006-01-19T11:17+0100 Gokhan A. wrote:

regex?
u.password.size > 0}
Eh? I don’t need confirmation. I just want to make :image_url an
optional requirement in the form, as well as checking input IF
:image_url is entered.

Kai H. wrote:

I am just going through the book and I come across a problem with the
code in the app/models:

validates_format_of :image_url,
    :with => %r{^http:.+\.(gif|jpg|png)$}i,
    :message => "must be a URL for a GIF, JPG, or PNG image"

It seems to moan when it’s empty even though I removed :image_url from
validates_presence_of. How should I make it optional? Altering the
regex?

Btw I have the same sort of problem with example code for the price
example. I want it optional unless I used validates_presence_of. Isn’t
that sensible?

In the SQL DDL, does NOT NULL mean anything here?

This works for confirmation and I guess it would work in your case too

validates_confirmation_of :password, :if=> Proc.new { |u|
u.password.size > 0}

Regards
Gokhan A.
Web D.
www.sylow.net

On 1/19/06, Kai H. [email protected] wrote:

validates_presence_of. How should I make it optional? Altering the
validates_confirmation_of :password, :if=> Proc.new { |u|
u.password.size > 0}

Eh? I don’t need confirmation. I just want to make :image_url an
optional requirement in the form, as well as checking input IF
:image_url is entered.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

He’s pointing out that you can pass it an :if option… So you’d want
to do something like

 validates_format_of :image_url,
     :with => %r{^http:.+\.(gif|jpg|png)$}i,
     :message => "must be a URL for a GIF, JPG, or PNG image",
     :if => Proc.new { |m| !m.image_url.nil? and m.image_url.length 

0 }

Not sure if that’ll do it, but hopefully it gets you going on the
right path. Check out the API docs for the validation methods.

Pat

On Thu, 2006-01-19 at 21:12 -0700, Pat M. wrote:

It seems to moan when it’s empty even though I removed :image_url from

right path. Check out the API docs for the validation methods.


that was pointless in the book…I ended up just changing the line
:with => %r{^http:.+.(gif|jpg|png)$}i,
to
:with => %r{^.+.(gif|jpg|png)$}i,

and then I could move on because the http directive isn’t going to work
unless you put the files on a web server - pointless for the
demonstration and that is a flaw in the book.

Craig