I have my Request like this:
class RegisterRequest extends Request
{
public function rules()
{
$rules = [
'role' => [
'required',
'string',
'exists:sys_roles,slug',
],
'name' => [
// 'required_without_all:first_name,last_name',
'string',
'min:2',
],
'first_name' => [
'sometimes',
'string',
'min:2',
],
'last_name' => [
'sometimes',
'string',
'max:128',
],
'email' => [
'email',
// 'unique:sys_users,email',
],
'phone_number' => [
'sometimes',
'string',
'max:32',
],
'password' => [
'sometimes',
'min:6',
'confirmed',
],
'roles.*' => [
'sometimes',
'exists:sys_roles,id',
],
'profile' => [
'sometimes',
'nullable',
'array',
],
'document' => [
'required',
'string'
],
'address' => [
'required',
'array'
],
];
/**
* The username rules can valy accordinly with the user type
*/
switch ($this->input('role', Role::ADMIN)) {
case Role::USER:
$document_rule = IlluminateValidationRule::unique('sys_users', 'document')->where('status', true);
array_push($rules['document'], $document_rule);
$email_rule = IlluminateValidationRule::unique('sys_users', 'email')->where('status', true);
array_push($rules['email'], $email_rule);
break;
default:
case Role::ADMIN:
throw new UnexpectedValueException(trans('auth.wrong_role'));
}
return $rules;
}
public function withValidator($v)
{
// Adds the correct document validation Rule according to the profile entity_type
$v->sometimes('document', new AppRulesCpf, function ($input) {
return !empty($input->document) && $input->profile['entity_type'] === User::PRIVATE_ENTITY;
});
$v->sometimes('document', new AppRulesCnpj, function ($input) {
return !empty($input->document) && $input->profile['entity_type'] === User::LEGAL_ENTITY;
});
}
/**
* Performs the validation on Resolving (after we change the password to a hash to
* improve security and save directly on the DB)
*/
public function validate()
{
parent::validate();
if ($this->filled('password')) {
$hasher = Sentinel::getHasher();
$this->merge(['password' => $hasher->hash($this->input('password'))]);
}
}
}
And I’m sending the following JSON
{
"address":{
"zipcode":"14056-741",
"address":"Rua Antônia Pimentel da Silva",
"neighborhood":"Jardim Doutor Paulo Gomes Romeo",
"number":"555",
"complement":"",
"state":"SP",
"city":"Ribeirao Preto"
},
"name":"Tiago Ferezin",
"email":"[email protected]",
"emailConfirmation":"[email protected]",
"entity_type":"1",
"document":"325.157.142-55",
"date_of_birth":"17/06/1985",
"phone_number":"(99) 99999-9999",
"phone_number_alt":"(99) 99999-9999",
"password":"njskeennjje",
"password_confirmation":"njskeennjje",
"accept_to_receive_emails":0,
"terms":true
}
But the request returns error 422 with the following response
{
"errors": {
"email": [
"validation.unique"
],
"document": [
"validation.unique"
]
}
}
What could be happening and what is the best way to fix this error?
One of the attempts I made was to change the email and the document, hoping to pass validation and thus proceed with the request, but the error persisted.
New contributor
Tiago Ferezin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.