9

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.

3 Answers 3

20

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

-1

Writing The Validation Logic

public function store(Request $request)
{
    $validatedData = $request->validate([
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
 ]);

 // The blog post is valid...
}

Displaying The Validation Errors

<!-- /resources/views/post/create.blade.php -->

<h1>Create Post</h1>

@if ($errors->any())
    <div class="alert alert-danger">
       <ul>
          @foreach ($errors->all() as $error)
              <li>{{ $error }}</li>
          @endforeach
      </ul>
   </div>
@endif

<!-- Create Post Form -->

1 Comment

Sorry, I knew this $validatedData = $request->validate([...]). What i asked is validated data from Validator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.