# Validation: :if =\> ... does not work

**URL:** <https://www.ruby-forum.com/t/validation-if-does-not-work/93150>\
**Category:** Rails\
**Created:** [April 28, 2007, 10:24am UTC](https://www.ruby-forum.com/t/validation-if-does-not-work/93150 "2007-04-28T10:24:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 28, 2007, 10:24am UTC](https://www.ruby-forum.com/t/validation-if-does-not-work/93150/1 "2007-04-28T10:24:50Z")

</div>

Hi all

I’ve got the following model:

class Link \< ActiveRecord::Base  
attr\_accessible :url,  
:name,  
:description,  
:editors\_pick,  
:link\_category\_ids

validates\_presence\_of :url  
validates\_uniqueness\_of :url  
validates\_length\_of :url,  
:maximum =\> 100  
validates\_format\_of :url,  
:with =\>  
/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)_.[a-z]{2,5}(([0-9]{1,5})?/._)?$/ix,  
:if =\> Proc.new { |link| !link.errors.on(:url) }

validates\_presence\_of :name  
validates\_uniqueness\_of :name  
validates\_length\_of :name,  
:maximum =\> 40

has\_and\_belongs\_to\_many :link\_categories,  
:join\_table =\> :links\_are\_categorized\_by\_link\_categories

def before\_validation  
name.capitalize! if name # Capitalize name!  
self.url = ‘http://’ + self.url if !self.url.empty? and self.url !~  
/^http(s)?:///  
end  
end

I want only to check for the format of the url when there’s no other  
error message for this attribute, so I added the :if =\> … statement:

validates\_format\_of :url,  
:with =\>  
/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)_.[a-z]{2,5}(([0-9]{1,5})?/._)?$/ix,  
:if =\> Proc.new { |link| !link.errors.on(:url) }

Sadly this does not work: it displays both

```
* Url is invalid
* Url can't be blank

```

when leaving its field blank. ☹ What’s going wrong here?

Thanks for help, Josh
