mylesthe.dev
mylesthe.dev

Reputation: 9685

Laravel 4 Validation unique(database) ignore current

I'm doing validation on a users edit form and have the validation checking if the username is unique in the database or not.

However when the editing a user and you don't change the username field it's still checking if the field is unique or not...

Is there a laravel way to ignore the current username if not changed?

Upvotes: 20

Views: 11145

Answers (4)

hasib32
hasib32

Reputation: 835

This solution worked for me:

protected  $rules = array(
    'email' => "required|email|unique:users,email,:id",
);

Upvotes: 9

Derek
Derek

Reputation: 4751

For Laravel 4.1, if you extend the Illuminate\Validation\Validator class, override the validateUnique() method as such:

/**
 * Validate the uniqueness of an attribute value on a given database table.
 *
 * If a database column is not specified, the attribute will be used.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @param  array   $parameters
 * @return bool
 */
protected function validateUnique($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'unique');

    $table = $parameters[0];

    // The second parameter position holds the name of the column that needs to
    // be verified as unique. If this parameter isn't specified we will just
    // assume that this column to be verified shares the attribute's name.
    $column = isset($parameters[1]) ? $parameters[1] : $attribute;

    // If the specified ID column is present in the data, use those values; otherwise,
    // fall back on supplied parameters.
    list($idColumn, $id) = [null, null];
    if (isset($parameters[2])) {
        list($idColumn, $id) = $this->getUniqueIds($parameters);

        if (strtolower($id) == 'null') $id = null;
        else if (strtolower($id) == 'id' && isset($this->data[$idColumn])) $id = $this->data[$idColumn];
    }
    else if (isset($this->data['id'])) {
        $idColumn = 'id';
        $id = $this->data[$idColumn];
    }

    // The presence verifier is responsible for counting rows within this store
    // mechanism which might be a relational database or any other permanent
    // data store like Redis, etc. We will use it to determine uniqueness.
    $verifier = $this->getPresenceVerifier();

    $extra = $this->getUniqueExtra($parameters);

    return $verifier->getCount(

        $table, $column, $value, $id, $idColumn, $extra

    ) == 0;
}

In your validator rules, you could do this (will check for id field existing in validation data - this is the most generalized usage):

$rules = 'unique:model,field';

this (will check for id field existing in validation data) (equivalent to the above):

// specifying anything other than 'id' or 'null' as the 3rd parameter
// will still use the current behavior
$rules = 'unique:model,field,id'; 

or this (will check for idColumn field existing in validation data):

// specifying anything other than 'id' or 'null' as the 3rd parameter
// will still use the current behavior
$rules = 'unique:model,field,id,idColumn';

Upvotes: 1

mylesthe.dev
mylesthe.dev

Reputation: 9685

Should have read the docs more, of course laravel 4 does this...

What I had

$rules = array('username' => 'required|unique:users');

What I have

$rules = array('username' => 'required|unique:users,username,'.$id);

You can tell the validator to ignore a given ID like above.

Upvotes: 29

Bradley
Bradley

Reputation: 455

Hi you can use the validator as a service or copy the logic and apply that your need, like this

$valitator = MyCustomValidator::make()->on('update')->setCurrent($id); 

check in: https://github.com/bradleySuira/laravel-context-validator/blob/master/examples/unique-on-update.php

Upvotes: 0

Related Questions