Reputation: 469
In my CreateUserRequest for email it has the validation rule of unique, and it's the same for my EditUserRequest, but It keeps saying that the address is already registered. How do I exclude the email address of the user I am updating from the uniqueness in the rules? Otherwise I have to register a new email address every time the user is updated.
EditUserRequest:
namespace App\Http\Requests;
use App\Http\Requests\Request;
class EditUserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
public function messages()
{
return [
'unique'=>'That email address is already registered',
'regex'=>'Password must contain at least 1 letter and 1 number'
];
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name'=>'required|min:2|max:50',
'email'=>'required|email|unique:users,email|max:50'
//'password'=>'min:8|regex:/[a-zA-Z][0-9]/' NOT IN USE WHILE IN DE
];
}
}
CreateUserRequest:
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateUserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
public function messages()
{
return [
'unique'=>'That email address is already registered',
'regex'=>'Password must contain at least 1 letter and 1 number'
];
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name'=>'required|min:2|max:50',
'email'=>'required|email|unique:users,email|max:50',
'password'=>'required'
//'password'=>'required|min:8|regex:/[a-zA-Z][0-9]/' NOT IN USE WHILE IN DE
];
}
}
Upvotes: 0
Views: 679
Reputation: 469
All that was necessary was adding $user = User::find($this->users);
and then just appending $user->id;
as suggested. Was just missing finding the user in the first place.
Upvotes: 0
Reputation: 29
You need to change your EditUserRequest and add ID of the current record to be ignored.
Take a look at the official docs: https://laravel.com/docs/5.1/validation#rule-unique in the paragraph: Forcing A Unique Rule To Ignore A Given ID
Something like:
'email'=>'required|email|unique:users,email,'. $user->id .'|max:50'
Upvotes: 1