S
S
SnyMaster2019-05-07 20:25:18
Validation
SnyMaster, 2019-05-07 20:25:18

Is it possible to reuse a Validator instance?

Validators can be created using the Validator facade. For example, from the documentation:

$validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

Next, you can check the validation result and perform some actions:
if ($validator->fails()) {
            // что-то выполняем
        }

Validator has a setData method that allows you to set the validation data set. It is possible to replace the dataset after the first run of validation by using the setData method and using the existing Valdator instance to validate the new dataset.
Doesn't this contradict the Validator ideology in Laravel? The fact is that neither in the documentation, nor in the examples of using the Validator anywhere, did I meet the REUSE of the created Validator instance. But I did not meet any descriptions of restrictions.
For example, the task in a console application is to receive user input in a loop. The application processes the input and waits for the next input, the application ends, for example, by "Ctrl+C".
The simplified code looks like this:
$message = $this->ask(‘You message:’);

$validator = Validator::make([
 ‘message’ => $message,
], [
 ‘message’ => [‘required|max:200’],
]);

while (true){
 if ($validator->fails()) {
 //show errors
 } else {
 // do something
 }
 $message = $this->ask(‘You message:’);
 $validator->setData([‘message’ => $message]);
}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
SnyMaster, 2019-05-23
@SnyMaster

So, this question was asked because of the behavior of the validator. It was necessary to understand what is the reason for the one-time use of an instance (instance) of Validator.
As I expected, the reason was banal - a bug.
A fix has been proposed https://github.com/laravel/framework/issues/28288. Not understood at first, but later realized and included as a fix at the suggestion of another developer https://github.com/laravel/framework/commit/0e52e4...
At the moment, there are no restrictions on the reuse of the Validator instance (instance) and no side effects.
And don't forget to update your Laravel version)

K
Kirill Nesmeyanov, 2019-05-07
@SerafimArts

Validator::make just creates a new instance.
PS1: In general, it is considered good practice to use contracts https://github.com/illuminate/contracts/blob/maste... via DI, and facades only in the most extreme cases (never).
PS2: In the particular case of validation, it is desirable to use form requests, not controllers.

A
Alex Wells, 2019-05-08
@Alex_Wells

The validator may have other props, which may change after the ->fails() call. This is a mutable object, so I wouldn't mess around with such bullshit, but would create a new validator instance (or correct my code so that such issues would not arise).

M
Mokhirjon Naimov, 2017-04-16
@Tarasovych

method:

public function show($slug)
{
    $blog     = Blog::where('slug', $slug)->firstOrFail();
    $category = Category::find($blog->category_id);

    return view('blog.show', compact('blog'));
}

route:
view:
@foreach ($blogs as $blog)
    <a href="{{ route('blog.show', $blog->slug) }}">{{ $blog->title }}</a>
@endforeach

D
D3lphi, 2017-04-16
@D3lphi

The second argument to the route() function is an array with query parameters:
{{ route('blog.show', ['id' => $blog->slug]) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question