Validation Unique PUT and POST UNIQUE in REQUEST Laravel

Asked

Viewed 36 times

-1

I have a problem validating the Unique field. My problem before was q if I was going to update the value I didn’t edit, because I was accusing q the value was Japanese, I solved it, but now it doesn’t validate the Unique field, but updates kkk

This is my Request - I’ve tried these 3 methods and they only let me update the field! It doesn’t validate if the value is unique before sending it to the bank!

public function rules()
{
    return[/*'nome'=>'bail|required|min:3|max:250|',Rule::unique('service')->ignore($this->service),*/

        'nome'=> 'bail|required|min:3|max:250|unique:service,nome,'.$this->id.',id',

        /*'nome' => 'bail|required|min:3|max:250',Rule::unique('service','nome')->ignore($this->service),*/
        'observacao'=> 'required'
    ];
}

Now my Controller

public function create()
{
    $action = route('admin.service.store');
    return view('admin.cadastro.service',compact('action'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\RedirectResponse
 */
public function store(ServiceRequest $request)
{
    Service::create($request->all());
    return  redirect()->route('admin.gerencial.index')->with('success_message', 'Cadastrado com Sucesso');
}

I’ve tried several ways and I can’t do it! I need help!

1 answer

0

I was able to solve my problem! As I imagined when running the validation in Rules in the UNIQUE field it only does a task or ignores or assigns! In this case he was just ignoring the ID when he was updating and did not validate Unique no create.

Rule::unique('service')->ignore($this->service)

Now to correct I had to do this validation within RULES in Request

$regras=[
        'campos do banco'          => 'bail|required|min:3|max:150',
        'endereco'      => 'required|min:3|max:200',
        'cities_id'     => 'required',
        'states_id'     => 'required'
    ];

    if ($this->method() === "POST"){
        $regras['coluna UNIQUE']= [
            'required',Rule::unique('client')
        ];
    }
    else{
        $regras['coluna UNIQUE']= [
        'required',Rule::unique('table')->ignore($this->route)
    ];
}
    return $regras;

Browser other questions tagged

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