Answer the question
In order to leave comments, you need to log in
Why doesn't custom validation work?
There is a custom rule for checking the date. Everything is according to the Laravel documentation, but it does not work and always returns that there are no errors. dump and dd from the passes method does not react in any way. Are there any guesses?
class CheckDeliveryDate implements Rule
{
public $client_id;
private $error_messge;
public function __construct(int $client_id)
{
$this->client_id = $client_id;
$this->error_messge = "Некорректная дата доставки";
}
public function passes($attribute, $value)
{
$delivery_date = Carbon::parse($value);
if ($delivery_date->isToday()) {
$this->error_messge = "Дата доставки не может быть сегодняшней";
return false;
}
if ($delivery_date->endOfDay()->isPast()) {
$this->error_messge = "Дата доставки не может быть прошедшей";
return false;
}
return true;
}
public function message() {
return $this->error_messge;
}
}
$validator = Validator::make(
$request->all(),
$this->rules($client_id)
);
public function rules($client_id) {
return [
'orders.*.positions.*.delivery_time' => ['required', 'date', new CheckDeliveryDate($client_id)],
];
}
Answer the question
In order to leave comments, you need to log in
I figured it out... It turned out that the JSON format was not quite correct. The validator works with an array, but not with a collection
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question