Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Validation unique(database) ignore current

Tags:

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?

like image 254
mylesthe.dev Avatar asked Jun 07 '13 04:06

mylesthe.dev


3 Answers

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.

like image 188
mylesthe.dev Avatar answered Oct 01 '22 06:10

mylesthe.dev


This solution worked for me:

protected  $rules = array(
    'email' => "required|email|unique:users,email,:id",
);
like image 30
hasib32 Avatar answered Oct 01 '22 04:10

hasib32


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';
like image 23
Derek Avatar answered Oct 01 '22 06:10

Derek