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 App\Rules;
use Closure;
use Illuminate\Contracts\Validation\Rule;
use App\Models\Product;
class SkuDeletedValidationRule implements Rule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $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 App\Models\Product;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use App\Rules\SkuDeletedValidationRule;
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, \Illuminate\Contracts\Validation\ValidationRule|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
what to fix in my custom validation to be this working well? thank you in advanced.
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): \Illuminate\Translation\PotentiallyTranslatedString $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;
}