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?
Should have read the docs more, of course laravel 4 does this...
$rules = array('username' => 'required|unique:users');
$rules = array('username' => 'required|unique:users,username,'.$id);
You can tell the validator to ignore a given ID like above.
This solution worked for me:
protected $rules = array(
'email' => "required|email|unique:users,email,:id",
);
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With