Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get validated data from Validator instance in Laravel?

I manually created a Validator, but i can't find a method to get the validated data.
For Request, validated data return from $request->validate([...])
For FormRequest, it's return from $formRequest->validated()
But with Validator, i don't see a method like those 2 above.

like image 786
kble Avatar asked Dec 10 '17 03:12

kble


People also ask

What does validate return in laravel?

XHR Requests & Validation When using the validate method during an XHR request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.

What is confirmed in laravel validation?

confirmed. The field under validation must have a matching field of foo_confirmation . For example, if the field under validation is password , a matching password_confirmation field must be present in the input.

How does laravel validation work?

The validate method accepts an incoming HTTP request and a set of validation rules. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user.


2 Answers

Assuming that you're using Validator facade:

use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), $rules, $messages, $attributes);

if ($validator->fails()) {
    return $validator->errors();
}

//If validation passes, return valid fields
return $validator->valid();

https://laravel.com/api/5.5/Illuminate/Validation/Validator.html#method_valid

like image 100
tylik Avatar answered Oct 17 '22 03:10

tylik


If you use the Validator facade with make it will return a validator instance. This validator instance has methods like validate(), fails() and so on. You can look those methods up in the validator class or in the laravel api-documentation.

like image 37
Tamali Avatar answered Oct 17 '22 01:10

Tamali