Validates_uniqueness_of but only when exists

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

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?

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