# Validates\_presence\_of

**URL:** <https://www.ruby-forum.com/t/validates-presence-of/52796>\
**Category:** Rails\
**Created:** [January 19, 2006, 5:22am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796 "2006-01-19T05:22:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kai\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kai\_H](https://www.ruby-forum.com/u/Kai_H)\
**Post date:** [January 19, 2006, 5:22am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796/1 "2006-01-19T05:22:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kai\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kai\_H](https://www.ruby-forum.com/u/Kai_H)\
**Post date:** [January 20, 2006, 5:46am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796/2 "2006-01-20T05:46:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kai\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kai\_H](https://www.ruby-forum.com/u/Kai_H)\
**Post date:** [January 19, 2006, 11:17am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796/3 "2006-01-19T11:17:35Z")

</div>

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](http://www.sylow.net)

---

<div class="post-metadata">

**Author:** ![Kai\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kai\_H](https://www.ruby-forum.com/u/Kai_H)\
**Post date:** [January 20, 2006, 5:52am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796/4 "2006-01-20T05:52:34Z")

</div>

On 1/19/06, Kai H. [removed\_email\_address@domain.invalid](mailto: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](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

---

<div class="post-metadata">

**Author:** ![Kai\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kai\_H](https://www.ruby-forum.com/u/Kai_H)\
**Post date:** [January 20, 2006, 6:01am UTC](https://www.ruby-forum.com/t/validates-presence-of/52796/5 "2006-01-20T06:01:38Z")

</div>

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
