A
A
andreyqin2014-11-21 17:43:27
Laravel
andreyqin, 2014-11-21 17:43:27

Laravel: Why doesn't the validator pass?

Good afternoon. I accept an array of the form:

$array = [
    'data' = [
        0 => [
            'title' => 'Название',
            'price' => 700,
            'quantity' => 5
        ],
        1 => [
            'title' => 'Название 2',
            'price' => 800,
            'quantity' => 3
        ],
        2 => [
            'title' => 'Название 3',
            'price' => 900,
            'quantity' => 2
        ] 
    ];
];

And the handler for this array:
public function myFunction()
{
    $input = Input::get('data');

    $validator = Validator::make($input, [
        'title'    => 'required',
        'price'    => 'required|numeric',
        'quantity' => 'required|numeric'
    ]);

    if ($validator->passes()) {
        // Сюда не попадает
    } else {
        // А вот сюда попадает
    }
}

The question is why is it not being validated?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Vinogradov, 2014-11-21
@pettson

Your data is an array.
As far as I know, the validator does not bypass the array. I think you need to make a loop that goes around the array, and already in it to do the validation of a specific element.

M
Mikhail Osher, 2014-11-21
@miraage

Variant from Stackoverflow .
Another option:
Iterate the collection up to the first error, or through the whole - as you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question