Validation of the Laravel 4 form must be unique in the update form, but not if the current

I am trying to check the profile profile of the update profile, according to which the check should check that the letter does not exist yet, but do not pay attention to whether existing users remain.

However, this continues to return a "This letter has already been received" verification error message.

I'm really not sure where I am going wrong. Otherwise, the update form works and updates perfectly.

HTML

{{ Form::text('email', Input::old('email', $user->email), array('id' => 'email', 'placeholder' => 'email', 'class' => 'form-control')) }} 

Route

 Route::post('users/edit/{user}', array('before' => 'admin', 'uses' => ' UserController@update ')); 

User model

 'email' => 'unique:users,email,{{{ $id }}}' 
0
source share
2 answers

Your rule is spelled correctly to ignore a specific identifier, however you need to update the value {{{ $id }}} in your unique rule before trying to validate.

I'm not necessarily a big fan of this method, but if your rules are a static attribute of the User object, you can create a static method that will hydrate and return rules with the correct values.

 class User extends Eloquent { public static $rules = array( 'email' => 'unique:users,email,%1$s' ); public static function getRules($id = 'NULL') { $rules = self::$rules; $rules['email'] = sprintf($rules['email'], $id); return $rules; } } 
+3
source

You can accomplish this with the sometimes validator function

Sort of:

 $validator->sometimes('email', 'unique:users,email', function ($input) { return $input->email == Input::get('email'); }); 

See http://laravel.com/docs/4.2/validation#conditionally-adding-rules for more details.

+1
source

Source: https://habr.com/ru/post/1211903/


All Articles