Custom request validating Unique field on update

Asked

Viewed 112 times

5

Good morning. Guys, I have a request created for user validation, but when I update a record it validates the Unique field I set in validation.. I tried to pass $this->id along so that it ignores, but without success.. someone has already gone through it.?

Laravel version: 8.0

Note: I am developing everything for API’s.

Controller code

public function update(UsuarioRequest $request, $id)
    {
        $usuario = Usuario::find($id);
        if($usuario){
            Usuario::find($id)->update($request->all());
            return response()->json("Usuario atualizado com sucesso", 201);
        }else{
            return response()->json("Usuario nao encontrado", 200);
        }
    }

Request code

 public function rules()
    {

        $rules = [
            'nome'              => 'required|max:100|min:2',
            'sobrenome'         => 'required|max:100|min:2',
            'cpf'               => 'required|unique:usuario',
            'rg'                => 'required|unique:usuario',
            'data_nascimento'   => 'required|date',
            'rua'               => 'required|max:300|min:2',
            'bairro'            => 'required|max:300|min:2',
            'cidade_id'         => 'integer',
            'semestre_id'       => 'integer',
            'periodo_letivo_id' => 'integer',
            'email'             => 'required|max:200|email|unique:usuario',
            'status'            => 'required|integer',
            'profissao_id'      => 'integer',
            'sexo_id'           => 'integer',
            'naturalidade_id'   => 'integer',
            'telefone1'         => 'required|max:45',
            'telefone2'         => 'max:45',
            'perfil_id'         => 'integer',
            'filial_id'         => 'integer'
        ];

        return $rules;
    }

2 answers

1


public function rules()
{

    $rules = [
        'nome' => 'required|max:100|min:2',
        'sobrenome' => 'required|max:100|min:2',
        'data_nascimento' => 'required|date',
        'rua' => 'required|max:300|min:2',
        'bairro' => 'required|max:300|min:2',
        'cidade_id' => 'integer',
        'semestre_id' => 'integer',
        'periodo_letivo_id' => 'integer',
        'email' => 'required|max:200|email|unique:usuario',
        'status' => 'required|integer',
        'profissao_id' => 'integer',
        'sexo_id' => 'integer',
        'naturalidade_id' => 'integer',
        'telefone1' => 'required|max:45',
        'telefone2' => 'max:45',
        'perfil_id' => 'integer',
        'filial_id' => 'integer'
    ];
    if (request()->id) {
        $rules['cpf'] = 'string';
        $rules['rg'] = 'string';
    } else {
        $rules['cpf'] = 'required|unique:usuario';
        $rules['rg'] = 'required|unique:usuario';
    }

    return $rules;
}

Try doing something like this, or create a request just to update

  • 2

    I don’t understand, I thought Ctrl+C Ctrl+V was simpler than an image...

  • Rafael, thank you very much. solved.

  • 2

    @Rafael Afonso, please avoid posting imagens code in your posts - questions and/or answers. To understand the real reasons read [Manual of how NOT to ask a question(https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions).

0

Browser other questions tagged

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