L
L
Leo Mavrin2020-10-23 17:33:38
Laravel
Leo Mavrin, 2020-10-23 17:33:38

Why doesn't data go through validation in laravel?

The situation is the following. Arrives json sent via fetch.
Json is valid, everything is OK, but the validation does not pass, although the controller is called and the data is put in the database (there is a method in the code below). To make sure the controller runs, I reverted to dd and it came back.
None of the following options work
Do not talk about the fact that the data for verification is different, it was necessary to at least somehow check.

public function validator(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'first_name' => 'first_name|unique:users',
            'last_name' => 'last_name|unique:users',
        ]);
        
        $this->validate($request, [
            'first_name' => 'required|unique:posts|max:255',
            'last_name' => 'required', //required - обязательно для заполнения
            'phone' => 'required|unique:user|min:11|max:11',
            'document_number' => 'required|unique:user|min:10|max:10',
            'password' => 'required|min:8',
        ]);
}


I did this: https://laravel.com/docs/5.0/validation#controller... Like
this: https://laravel.com/docs/5.0/validation#form-reque...

This was one of the first options :

$this->validate($request, [
            'first_name' => 'required|unique:posts|max:255',
            'last_name' => 'required',
            'phone' => 'required|unique:user|min:11|max:11',
            'document_number' => 'required|unique:user|min:10|max:10',
            'password' => 'required|min:8',
]);

How to validate data?
I need to return json with an array of errors if the data has not been validated. Now they pass anyway

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leo Mavrin, 2020-10-26
@Leo5878

To all Laravel martyrs)
Check how you submit the request. Because my problem was that I didn't deliver

'Accept': 'application/json'

This is how the request should look like:
And in api, in the route, you don’t need to write /api , in laravel, but in fetch you need to!
fetch("/api/register", {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'Accept': 'application/json'
        },
        body: json
}

P
pLavrenov, 2020-10-27
@pLavrenov


$ request- >validate( )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question