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.
29ab3f223a10c7b19916ea38141da74d?d=identicon&s=25 Kai Hendry (Guest)
on 2006-01-19 05: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?
54077fb03c95861af85880218cf7aef2?d=identicon&s=25 Gokhan Arli (sylow)
on 2006-01-19 11:17
Kai Hendry 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 Arli
Web Developer
www.sylow.net
29ab3f223a10c7b19916ea38141da74d?d=identicon&s=25 Kai Hendry (Guest)
on 2006-01-20 05:46
(Received via mailing list)
On 2006-01-19T11:17+0100 Gokhan Arli 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.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-01-20 05:52
(Received via mailing list)
On 1/19/06, Kai Hendry <hendry@iki.fi> 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
> Rails@lists.rubyonrails.org
> 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-20 06:01
(Received via mailing list)
On Thu, 2006-01-19 at 21:12 -0700, Pat Maddox 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.