Laravel更新模型具有唯一的属性validation规则

我有一个laravel User模型,它有一个唯一的usernameemailvalidation规则。 在我的知识库中,当我更新模型时,我重新validation这些字段,以便不需要规则validation的问题:

 public function update($id, $data) { $user = $this->findById($id); $user->fill($data); $this->validate($user->toArray()); $user->save(); return $user; } 

这在testing中失败

 ValidationException: {"username":["The username has already been taken."],"email":["The email has already been taken."]} 

有没有一种方法来优雅地解决这个问题?

追加当前正在更新的实例的id给validation器。

  1. 传递您的实例的id忽略唯一的validation器。

  2. 在validation器中,使用参数来检测您是否正在更新创build资源。

如果更新,则强制该唯一规则忽略给定的ID:

 //rules 'email' => 'unique:users,email_address,' . $userId, 

如果创build,照常进行:

 //rules 'email' => 'unique:users,email_address', 

另一种优雅的方式

在你的模型中,创build一个静态函数:

 public static function rules ($id=0, $merge=[]) { return array_merge( [ 'username' => 'required|min:3|max:12|unique:users,username' . ($id ? ",$id" : ''), 'email' => 'required|email|unique:member'. ($id ? ",id,$id" : ''), 'firstname' => 'required|min:2', 'lastname' => 'required|min:2', ... ], $merge); } 

validation创build:

 $validator = Validator::make($input, User::rules()); 

validation更新:

 $validator = Validator::make($input, User::rules($id)); 

validation更新,还有一些附加规则:

 $extend_rules = [ 'password' => 'required|min:6|same:password_again', 'password_again' => 'required' ]; $validator = Validator::make($input, User::rules($id, $extend_rules)); 

尼斯。

在我的问题中工作:

 public function update($id, $data) { $user = $this->findById($id); $user->fill($data); $this->validate($user->toArray(), $id); $user->save(); return $user; } public function validate($data, $id=null) { $rules = User::$rules; if ($id !== null) { $rules['username'] .= ",$id"; $rules['email'] .= ",$id"; } $validation = Validator::make($data, $rules); if ($validation->fails()) { throw new ValidationException($validation); } return true; } 

就是我所做的,基于上面接受的答案。

编辑:与forms请求,一切都变得更简单:

 <?php namespace App\Http\Requests; class UpdateUserRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required|unique:users,username,'.$this->id, 'email' => 'required|unique:users,email,'.$this->id, ]; } } 

您只需要将UpdateUserRequest传递给您的更新方法,并确保POST模型ID。

Laravel 5兼容和通用的方式:

我只是遇到了同样的问题,并以通用的方式解决了这个问题。 如果您创build的项目使用默认规则,如果更新项目,它将检查您的规则:unique并自动插入排除(如果需要)。

创build一个BaseModel类,让所有的模型inheritance它:

 <?php namespace App; use Illuminate\Database\Eloquent\Model; class BaseModel extends Model { /** * The validation rules for this model * * @var array */ protected static $rules = []; /** * Return model validation rules * * @return array */ public static function getRules() { return static::$rules; } /** * Return model validation rules for an update * Add exception to :unique validations where necessary * That means: enforce unique if a unique field changed. * But relax unique if a unique field did not change * * @return array; */ public function getUpdateRules() { $updateRules = []; foreach(self::getRules() as $field => $rule) { $newRule = []; // Split rule up into parts $ruleParts = explode('|',$rule); // Check each part for unique foreach($ruleParts as $part) { if(strpos($part,'unique:') === 0) { // Check if field was unchanged if ( ! $this->isDirty($field)) { // Field did not change, make exception for this model $part = $part . ',' . $field . ',' . $this->getAttribute($field) . ',' . $field; } } // All other go directly back to the newRule Array $newRule[] = $part; } // Add newRule to updateRules $updateRules[$field] = join('|', $newRule); } return $updateRules; } } 

现在,您已经像以前一样在您的模型中定义了您的规则:

 protected static $rules = [ 'name' => 'required|alpha|unique:roles', 'displayName' => 'required|alpha_dash', 'permissions' => 'array', ]; 

并validation他们在你的控制器。 如果模型没有validation,它将自动redirect到带有相应validation错误的表单。 如果没有发生validation错误,它将继续执行后面的代码。

 public function postCreate(Request $request) { // Validate $this->validate($request, Role::getRules()); // Validation successful -> create role Role::create($request->all()); return redirect()->route('admin.role.index'); } public function postEdit(Request $request, Role $role) { // Validate $this->validate($request, $role->getUpdateRules()); // Validation successful -> update role $role->update($request->input()); return redirect()->route('admin.role.index'); } 

而已! :)注意在创build时我们调用Role::getRules() ,在编辑时我们调用$role->getUpdateRules()

我有BaseModel类,所以我需要更通用的东西。

 //app/BaseModel.php public function rules() { return $rules = []; } public function isValid($id = '') { $validation = Validator::make($this->attributes, $this->rules($id)); if($validation->passes()) return true; $this->errors = $validation->messages(); return false; } 

在用户类中,让我们假设我只需要电子邮件和名称进行validation:

 //app/User.php //User extends BaseModel public function rules($id = '') { $rules = [ 'name' => 'required|min:3', 'email' => 'required|email|unique:users,email', 'password' => 'required|alpha_num|between:6,12', 'password_confirmation' => 'same:password|required|alpha_num|between:6,12', ]; if(!empty($id)) { $rules['email'].= ",$id"; unset($rules['password']); unset($rules['password_confirmation']); } return $rules; } 

我testing了这个与phpunit和工作正常。

 //tests/models/UserTest.php public function testUpdateExistingUser() { $user = User::find(1); $result = $user->id; $this->assertEquals(true, $result); $user->name = 'test update'; $user->email = 'ddd@test.si'; $user->save(); $this->assertTrue($user->isValid($user->id), 'Expected to pass'); } 

我希望能帮助别人,即使有更好的想法。 感谢您分享你的。 (在Laravel 5.0上testing)

一个angular色更新的简单例子


 // model/User.php class User extends Eloquent { public static function rolesUpdate($id) { return array( 'username' => 'required|alpha_dash|unique:users,username,' . $id, 'email' => 'required|email|unique:users,email,'. $id, 'password' => 'between:4,11', ); } } 

 // controllers/UsersControllers.php class UsersController extends Controller { public function update($id) { $user = User::find($id); $validation = Validator::make($input, User::rolesUpdate($user->id)); if ($validation->passes()) { $user->update($input); return Redirect::route('admin.user.show', $id); } return Redirect::route('admin.user.edit', $id)->withInput()->withErrors($validation); } } 

独特的validation与不同的列ID在Laravel

 'UserEmail'=>"required|email|unique:users,UserEmail,$userID,UserID" 

我为存储和更新调用不同的validation类。 在我的情况下,我不想更新每个领域,所以我有基本规则的创build和编辑的公共领域。 为每个添加额外的validation类。 我希望我的例子是有帮助的。 我正在使用Laravel 4。

模型:

 public static $baseRules = array( 'first_name' => 'required', 'last_name' => 'required', 'description' => 'required', 'description2' => 'required', 'phone' => 'required | numeric', 'video_link' => 'required | url', 'video_title' => 'required | max:87', 'video_description' => 'required', 'sex' => 'in:M,F,B', 'title' => 'required' ); public static function validate($data) { $createRule = static::$baseRules; $createRule['email'] = 'required | email | unique:musicians'; $createRule['band'] = 'required | unique:musicians'; $createRule['style'] = 'required'; $createRule['instrument'] = 'required'; $createRule['myFile'] = 'required | image'; return Validator::make($data, $createRule); } public static function validateUpdate($data, $id) { $updateRule = static::$baseRules; $updateRule['email'] = 'required | email | unique:musicians,email,' . $id; $updateRule['band'] = 'required | unique:musicians,band,' . $id; return Validator::make($data, $updateRule); } 

控制器:存储方法:

 public function store() { $myInput = Input::all(); $validation = Musician::validate($myInput); if($validation->fails()) { $key = "errorMusician"; return Redirect::to('musician/create') ->withErrors($validation, 'musicain') ->withInput(); } } 

更新方法:

 public function update($id) { $myInput = Input::all(); $validation = Musician::validateUpdate($myInput, $id); if($validation->fails()) { $key = "error"; $message = $validation->messages(); return Redirect::to('musician/' . $id) ->withErrors($validation, 'musicain') ->withInput(); } } 
 public static function custom_validation() { $rules = array('title' => 'required ','description' => 'required','status' => 'required',); $messages = array('title.required' => 'The Title must be required','status.required' => 'The Status must be required','description.required' => 'The Description must be required',); $validation = Validator::make(Input::all(), $rules, $messages); return $validation; } 

我有同样的问题。 我所做的:添加我的视图隐藏字段与模型的ID和validation器检查唯一,只有当我从视图中获得一些ID。

 $this->validate( $request, [ 'index' => implode('|', ['required', $request->input('id') ? '' : 'unique:members']), 'name' => 'required', 'surname' => 'required', ] ); 
 'email' => [ 'required', Rule::exists('staff')->where(function ($query) { $query->where('account_id', 1); }), ], 'email' => [ 'required', Rule::unique('users')->ignore($user->id)->where(function ($query) { $query->where('account_id', 1); }) ], 

你可以尝试下面的代码

 return [ 'email' => 'required|email|max:255|unique:users,email,' .$this->get('id'), 'username' => 'required|alpha_dash|max:50|unique:users,username,'.$this->get('id'), 'password' => 'required|min:6', 'confirm-password' => 'required|same:password', ]; 

如果你有另一个列被用作外键或索引,那么你必须在这样的规则中指定它。

 'phone' => [ "required", "phone", Rule::unique('shops')->ignore($shopId, 'id')->where(function ($query) { $query->where('user_id', Auth::id()); }), ],