Forum: Ruby on Rails validates_presence_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kai H. (Guest)
on 2006-01-19 06:22
(Received via mailing list)
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?
Gokhan A. (Guest)
on 2006-01-19 12:17
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
Kai H. (Guest)
on 2006-01-20 06:46
(Received via mailing list)
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.
Pat M. (Guest)
on 2006-01-20 06:52
(Received via mailing list)
On 1/19/06, Kai H. <removed_email_address@domain.invalid> 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
> removed_email_address@domain.invalid
> 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
Craig W. (Guest)
on 2006-01-20 07:01
(Received via mailing list)
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
This topic is locked and can not be replied to.