需要幫助更新我的驗證規則中的唯一規則。我有一個抽象驗證器,它將在存儲到我的數據庫和規則數組中之前驗證規則,我在創建或註冊用戶時將電子郵件設置爲唯一的,但在更新用戶時,如果電子郵件歸屬於用戶。Laravel 4獨特的驗證規則不起作用
抽象類驗證
abstract class Validator {
protected $errors;
protected $attributes;
public function __construct($attributes = null)
{
$this->attributes = $attributes ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->attributes, $this->rules());
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
public function getErrors()
{
return $this->errors;
}
}
驗證規則(UserRule.php)
use MyCustomValidatorNamespaceHere....
class UserRules extends Validator
public function rules()
{
return [
'email' => 'required|email|unique:users,email,id',
...
];
}
,並在我的UserController中我注入UserRule在constractor。 (UserRule $ userRule)。這是更新方法中的代碼。
public function update($id)
{
$if ($this->userRule->passes())
{
$this->user->find($id)->update(Input::all());
return .........
}
}
但驗證總是失敗並顯示電子郵件已被採取的錯誤。請幫助傢伙。
我解決了這個問題[這裏計算器(http://stackoverflow.com/a/28254548/1532168 )以通用的方式。如果您進行更新,它會自動調整您的規則,並在必要時爲「:unique」添加例外。 – cgross 2015-01-31 17:42:39