Level 122
your rule is CustomerNeededDocument - but you dont use it?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey i have senerio where i will post some fields like documents that has document id it will then check in Models whether the expiry date is required or not if it is required it shows error if i won't send anything. I have created a custom rule for that to handle bussiness logic but when i won't send field it won't trigger the custom rule. Here is my rule
<?php
namespace App\Library\Rules\Documents;
use CarRental\Models\CustomerNeededDocument;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class ValidDocumentIdentifyerNumber implements ValidationRule
{
protected $documentId;
public function __construct($documentId)
{
$this->documentId = $documentId;
}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$document = CustomerNeededDocument::where('id', $this->documentId)->first();
if ($document && $document->has_identify_number && !isset($value)) {
$fail('The selected :attribute is required.');
}
}
}
Here is my request
<?php
namespace CarRental\Requests\Customer\Documents;
use App\Library\Rules\Documents\ValidDocumentExpiryDate;
use App\Library\Rules\Documents\ValidDocumentIdentifyerNumber;
use CarRental\Models\CustomerNeededDocument;
use CarRental\Rules\Documents\ValidDocumentId;
use Illuminate\Foundation\Http\FormRequest;
class AddCustomerDocument extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'document_id' => ['required', new ValidDocumentId()],
'image' => ['required'],
'identify_number' => [new ValidDocumentIdentifyerNumber($this->input('document_id'))],
'expiry_date' => [new ValidDocumentExpiryDate($this->input('document_id'))],
];
}
}
Please or to participate in this conversation.