Forum: Ruby on Rails validates_uniqueness_of but only when exists

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.
Glenn C. (Guest)
on 2006-05-26 13:29
I want to validate the uniqueness of a field if the field has a value.
Is there anyway to do it?
David (Guest)
on 2006-05-26 13:33
Glenn C. wrote:
> I want to validate the uniqueness of a field if the field has a value.
> Is there anyway to do it?

Sure, you can use the following syntax:

validates_presence_of :field, :if => ..

So you can do anything you would do in a normal if.

David
Glenn C. (Guest)
on 2006-05-26 13:59
David wrote:
> Glenn C. wrote:
>> I want to validate the uniqueness of a field if the field has a value.
>> Is there anyway to do it?
>
> Sure, you can use the following syntax:
>
> validates_presence_of :field, :if => ..
>
> So you can do anything you would do in a normal if.
>
> David

Thanks, working well.

validates_uniqueness_of :employeenumber,  :if => employeenumber?
This topic is locked and can not be replied to.