Create a Request validation in Laravel with the Unique rule

Asked

Viewed 1,872 times

1

I created a request with the command:

php Artisan make:request Nameserver

And in the controller I’m calling him so:

public function nomeDaFuncao(NomedoRequest $request)
{

}

Only I need to use this request in two situations, both when creating some data, and when editing, but one of these fields I need to be unique in the database. In Laravel has the validation Unique that allows me to say that that data needs to be unique, only if I try to pass the same data at the time of editing, it presents error speaking q is equal.

I found this feature

Rule::Unique('table')->ignore($email)

But I don’t know how to pass this $email variable to the request I created and even passing it, it will only work for one situation, or edit, or create. Function of the request:

public function rules()
{
    return [
        'name' => 'required|max:255',
        'cpf' => 'required',
        'phone' => 'required',
        'birthdate' => 'required',
        'sex' => 'required',
        'email' => ['required', Rule::unique('users')->ignore($variavel)],
    ];
}

3 answers

1


You can configure the rules according to the method used.

Within Formrequest you have access to request(), route(), method(), etc....

public function rules()
    {

        $regras = [
            'name' => 'required|max:255',
            'cpf' => 'required',
            'phone' => 'required',
            'birthdate' => 'required',
            'sex' => 'required'
        ];
        // store
        if ($this->method() === "POST") {
            $regras['email'] = [
                'required',
                Rule::unique('users')
            ];
        } 

         // update
        else {
            $regras['email'] = [
                'required',
                Rule::unique('users')->ignore($this->request->get('email'), 'email')
            ];
        }
        return $regras;
    }

0

When you are inside a Classerequest, the $this is the request itself. So you can reference something that was sent in your form using:

$this->campo_do_form

So inside your Classerequest do so:

public function rules()
{
    $validations  = [
        'name' => 'required|max:255',
        'cpf' => 'required',
        'phone' => 'required',
        'birthdate' => 'required',
        'sex' => 'required',
        'email' => 'required|email|unique:nome_da_tabela,email',
    ];

    if($this->method('PUT')) {
        // para uma tabela SEM campos created_at,updated_at e deleted_at
        $validations['email'] .= ',' . $this->pk_da_tabela . ',pk_da_tabela';

        // para uma tabela COM campos created_at,updated_at e deleted_at
        $validations['email'] .= ',' . $this->pk_da_tabela . ',pk_da_tabela,deleted_at,NULL';
    }

    return $validations;
}

reference: https://laravel.com/docs/5.5/validation#Rule-Unique

0

Hit put the Rule in your Validator when performing the update just don’t put Unique, that the Unique only goes in create

protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'email' => 'required|unique:users',
        ],
        ValidatorInterface::RULE_UPDATE => [
            'email' => 'required',
        ],
    ];

    protected $messages = [
        'email.unique' => 'E-mail já cadastrado',
    ];

And on your controller do

    public function __construct(MinhaClasseValidator $validator) {
      $this->validator = $validator;
    }

public function create(Request $request) {
    $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.