Laravel 4 验证唯一(数据库)忽略当前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16976207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Validation unique(database) ignore current
提问by Mediabeastnz
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?
如果没有更改,是否有一种 Laravel 方法可以忽略当前用户名?
回答by Mediabeastnz
Should have read the docs more, of course laravel 4 does this...
应该更多地阅读文档,当然,laravel 4 是这样做的......
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.
您可以告诉验证器忽略上面的给定 ID。
回答by hasib32
This solution worked for me:
这个解决方案对我有用:
protected $rules = array(
'email' => "required|email|unique:users,email,:id",
);
回答by Derek
For Laravel 4.1, if you extend the Illuminate\Validation\Validator
class, override the validateUnique()
method as such:
对于 Laravel 4.1,如果您扩展Illuminate\Validation\Validator
该类,请按如下validateUnique()
方式覆盖该方法:
/**
* 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):
在您的验证器规则中,您可以这样做(将检查id
验证数据中存在的字段 - 这是最普遍的用法):
$rules = 'unique:model,field';
this (will check for id
field existing in validation data) (equivalent to the above):
这(将检查id
验证数据中存在的字段)(相当于上面的):
// 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):
或者这个(将检查idColumn
验证数据中存在的字段):
// specifying anything other than 'id' or 'null' as the 3rd parameter
// will still use the current behavior
$rules = 'unique:model,field,id,idColumn';
回答by Bradley
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
签入:https: //github.com/bradleySuira/laravel-context-validator/blob/master/examples/unique-on-update.php