Validates_uniqueness_of but only when exists


#1

I want to validate the uniqueness of a field if the field has a value.
Is there anyway to do it?


#2

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?


#3

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