R
R
Rodion Almetov2019-04-29 12:57:28
Laravel
Rodion Almetov, 2019-04-29 12:57:28

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;
    }
}

I connect in the controller:
$validator = Validator::make(
    $request->all(),
    $this->rules($client_id)
);

Rule array method:
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

2 answer(s)
R
Rodion Almetov, 2019-04-30
@radar4ick

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

N
NubasLol, 2019-04-29
@NubasLol

dump and dd from the passes method does not react in any way

Because it is not called, so it does not work. Error here
Show what you are passing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question