简体   繁体   中英

Custom validation using laravel 5.8

Good day,

I am trying to update brand data using Laravel 5.8 and I made a custom validation function to validate the name of the brand but my problem is when I attempt an update, the validation fails and I get a message saying Opps name Is Exist Before

I need to update this validation function to link the brand id with the brand name to perform the update without showing the validation error.

Thanks in advance.

Here is my code:

public function update(Request $request, $id)
{
    //prepare data for validation
    request()->validate([
        'name'  => [
            'required',
            'min:2', // validate english name is exist before
            function ($attribute, $value, $fail) {
                $englishname=Brand::where(['name'=>$value,'deleted'=>1 ])->first();
                if(false !=$englishname) {
                    $fail('Opps '.$attribute.' Is Exist Before.');
                }
            },
        ],
        'keywords' => 'required|min:2',
        'ar_name'  => [
            'required',
            'min:2',// validate english name is exist before
            function ($attribute, $value, $fail) {
                $arname=Brand::where(['ar_name'=>$value,'deleted'=>1])->first();
                if(false !=$arname) {
                    $fail('Opps '.$attribute.' Is Exist Before.');
                }
            },
        ],
        'ar_keywords' => 'nullable',
        'status' => 'required|integer',
    ],[],[
        "name"=>"Brand Name",
        'keywords' => 'Brand KeyWords',
        'ar_name' => 'اسم الماركة',
        'ar_keywords' => 'الكلمات الدليلية',

    ]);
    // start pass data to model
    $branddata=array(
        'name'          =>$request->name,
        'keywords'      =>$request->keywords,
        'ar_name'       =>$request->ar_name,
        'ar_keywords'   =>$request->ar_keywords,
        'last_updated_by'=>auth()->user()->id,
        'status'        =>$request->status,
    );

    //start update data
    $updateddata=Brand::where(['id'=>$id,'deleted'=>1])->update($branddata);
    if (false !==Brand::create($updateddata))
    {
        return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated  Successfully");

    }else{
        return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated Successfully");

    }


}

You can use the unique validation rule from Laravel with extended attributes passed for the validation checks.

If you want to validate that the name attribute must be unique (but allows for the same model instance), you can do something like follow:

'name' => "required|string|email|unique:<table name for the Brand::class>,name,{$id}"

This validation rule will check for uniqueness of the provided name for every rows except the one with $id as its primary key.

If your $id variable is not a primary key, you can specify the column name for the variable as follows:

'name' => "required|string|email|unique:<table name for the Brand::class>,name,{$id},<column name of the id>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM