# Making Validations Optional

**URL:** <https://www.ruby-forum.com/t/making-validations-optional/111447>\
**Category:** Rails\
**Created:** [August 7, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/making-validations-optional/111447 "2007-08-07T14:17:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 7, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/making-validations-optional/111447/1 "2007-08-07T14:17:48Z")

</div>

Hello,

I am working with URL validations. I want this validation to be  
optional if the data exists in the form submission. I have tried  
several things.

validates\_format\_of :website,  
:if =\> params[:organization][:website],  
:with =\>  
/((http|https)://)?[a-z0-9]+([-.]{1}[a-z0-9]+)_.[a-z]  
{2,5}(([0-9]{1,5})?/._)?/,  
:message =\> “Please enter a valid url”

Of course the challenge is in the :if statement

I won’t bother you guys with all of my experiments, but I have tried  
using :website, :website.length \> 0, params[:organization][:website]  
and creating a method in my organization class. None of these have  
worked. What am I missing?

Thanks!

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 7, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/making-validations-optional/111447/2 "2007-08-07T14:38:55Z")

</div>

You cannot access params in model.

On 8/7/07, Mindtonic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> {2,5}(([0-9]{1,5})?/.\*)?/,
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 7, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/making-validations-optional/111447/3 "2007-08-07T15:07:59Z")

</div>

Ahhh… so I was barking up the wrong proverbial tree.

The solution to the problem lay in the REGEX code. I simply added a  
test for zero or one instance at the end:

/(((http|https)://)?[a-z0-9]+([-.]{1}[a-z0-9]+)_.[a-z]{2,5}(([0-9]  
{1,5})?/._)?)\*/

Thanks.
