M
M
Mykola Ivashchuk2017-08-09 18:45:26
Laravel
Mykola Ivashchuk, 2017-08-09 18:45:26

How to validate data in API?

It is necessary to validate the data and the fields themselves in the API, I tried to use FormRequest, after that I receive this object in the controller method. sample code:

class GetReqData extends FormRequest implements InterfaceGetTripsListRequest
{

    public function authorize()
    {
        return true;
    }

    public function validate()
    {
        return [
            'user_id' => 'required|integer'
        ];
    }

    public function getUserId():int
    {
        return (int)$this->get('user_id');
    }
}

but for example, if you do not pass the user_id field, then there is no error that this field is not specified.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2017-08-09
@JhaoDa

And why suddenly the validation rules are described in the method validate()?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question