You could have two request objects - CreateUserRequest and EditUserRequest, as essentially those are the two different requests.
Mar 24, 2015
33
Level 18
userRequest add and edit differences
Hi guys,
In my backend i can add and edit users, when adding a user the password and email are both required fields, where the email is also unique.
Works fine for adding, but i can't figure out how to get it to work with editing, since when i edit a user he is allowed to have the password field empty, and is allowed to use the e-mail address he already has.
However upon submitting i get the message that the e-mail is already in use.
use App\Http\Requests\Request;class UserRequest 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()
{
$rules = [
'name' => 'required|min:3|max:25',
'password' => 'required|min:5|max:25',
'email' => 'required|email|unique:users',
'active' => 'required',
'type' => 'required'
];
if ($this->method() == 'PATCH')
{
$rules['password'] = 'max:25';
$rules['email'] = 'required|email|unique:users,email,'. $this->get('id');
}
return $rules;
}
}
Level 52
If parameter name is id in route just do :
$rules['email'] = 'required|email|unique:users,email,'. $this->id;
If you have a resource would be :
$rules['email'] = 'required|email|unique:users,email,'. $this->user;
Also you can use this syntax for the verb :
if($this->isMethod('patch'))
Please or to participate in this conversation.