skip to Main Content

I have custom validation for my form request. When users input the value and the value exists also field deleted_at is not null this validation should be working.

the problem is even when I input the value that is not exists and deleted_at is null. the validation still working.

this is what i have done

customValidation:

<?php

namespace AppRules;

use Closure;
use IlluminateContractsValidationRule;
use AppModelsProduct;

class SkuDeletedValidationRule implements Rule
{
    /**
     * Run the validation rule.
     *
     * @param  Closure(string): IlluminateTranslationPotentiallyTranslatedString  $fail
     */
    public function passes($attribute, $value)
    {
        // Retrieve the model instance based on the SKU value
        $model = Product::where('sku', $value)->first();

        // Check if the model exists and its deleted_at is not null
        if( $model && $model->deleted_at !== null){
            return true;
        }
        return false;
    }

    public function message()
    {
        return 'The :attribute is exists but has been deleted, please restore it.';
    }
}

my form request:

use AppModelsProduct;
use IlluminateFoundationHttpFormRequest;
use IlluminateSupportStr;
use AppRulesSkuDeletedValidationRule;


class ProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth('cms-admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, IlluminateContractsValidationValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required',
            'sku' => [new SkuDeletedValidationRule, 'required' . $this->product?->id],
            'sku_merchant' => 'nullable|unique:products,sku_merchant,' . $this->product?->id,
            'slug' => 'nullable',
            'is_publish' => 'required',
            'price' => 'required',
            'description' => 'required',
            'discount' => 'nullable',
            'images.*' => 'required',
            'category.*' => 'nullable|exists:categories,id',
            'supplier_id' => 'required',
            'buyer_price' => 'required'
        ];
    }
}

my Controller:

    public function store(ProductRequest $request)
    {
        $this->productService->create($request->validated());

        return redirect(route('admin.product.index'))->with('success', 'product created!');
    }

when i input the non existing value

non existing value input

what to fix in my custom validation to be this working well? thank you in advanced.

2

Answers


  1. I think you are using SoftDelete traits and schema.

    You can’t add new sku if you have already deleted it you have two options.

    Try this if previously deleted.

    Product::withTrashed()->where('sku', $value)->restore();
    

    Or you can force delete it before inserting new

    Product::where('sku', $value)->forceDelete();
    
    Login or Signup to reply.
  2. To retrieve data that has been softly deleted, you need to use withTrashed() method.

    Simply modify the following line:

    $model = Product::where('sku', $value)->first();
    

    To

    $model = Product::withTrashed()->where('sku', $value)->first();
    

    And based on your verification message, I assume you should return false when you find data with the same SKU that has been softly deleted.

    /**
     * Run the validation rule.
     *
     * @param  Closure(string): IlluminateTranslationPotentiallyTranslatedString  $fail
     */
    public function passes($attribute, $value)
    {
        // Retrieve the model instance based on the SKU value
        $model = Product::withTrashed()->where('sku', $value)->first();
    
        // Check if the model exists and its deleted_at is not null
        if( $model && $model->deleted_at !== null){
            return false;
        }
        return true;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search