Control the transition destination at the time of error with Laravel validation

Sep 9, 2020 PHP Laravel

Problem

Cause

** When applying validation with Laravel, when returning to the screen at the time of moving in when an error occurs, it will be transitioned with the GET method **

Since I used the validate method of Request and applied validation, I could set rules and error messages, but I could not control after that, and the problem like the beginning occurred. Was there. Looking at Validation 5.5 Laravel, there was a way to set validation other than Request, so I tried it. Now that the problem has been resolved, I’ll describe how to do it.

solution

** Use validator **

validator

validator is used by directly filling in the controller. This shape is the basis.

use Validator;

class SampleController extends Controller
{
    public function sample (Request $ request)
    {
        $ validator = Validator :: make ($ request-> all (), [
                // Fill in validation rules
            ],,
            [
                // Fill in the error message
            ]);

        if ($ validator-> fails ()) {
            return redirect ('home')
                        -> withErrors ($ validator)
                        -> withInput ();
            // Processing at the time of error
        }
        // Processing when validation passes
    }
}

** Specify validation rules and error messages with Validator :: make **, just like the Validate method of Request. ** ** If the validation rule declared with Validator :: make is not met, $ validator-> fails () {} will be executed.

By specifying the redirect destination using ** Validator **, the specification of the transition destination page seems to be resolved. By the way, you can also specify the value specified by ** name () ** of routes \ web.php to specify the redirect destination.

Route :: get ('/ home','HomeController @ index')-> name ('home');

Validator customization

It is assumed that the project of Laravel is created in the flow of information edit screeninformation edit confirmation screen edit completion screen. At that time, there may be a limit to the information that can be passed with redirect (). However, ** $ validator-> fails () {} ** can specify not only redirect (). An example is described below.

use Validator;

class EditCheckController extends Controller
{
    public function sample (Request $ request)
    {
        $ validator = Validator :: make ($ request-> all (), [
                'name' =>' required | max: 20',
                'email' =>' required | email',
                'body' =>'required',
                'delete_flag' =>'boolean',
                // Fill in validation rules
            ],,
            [
                'name.required' =>'Please enter a name. ',
                'name.max' =>' Please enter the name within 20 characters. ',
                'email.required' =>'Please enter an email. ',
                'email.email' =>'Please enter your email in email address format. ',
                'body.required' =>' Please enter the body. ',
                'delete_flag.boolean' =>'Enter 0 or 1. ',
                // Fill in the error message
            ]);

        if ($ validator-> fails ()) {
            $ edit_id = $ request-> input ('edit_id');
            $ name = $ request-> input ('edit_name');
            $ email = $ request-> input ('edit_email');
            $ body = $ request-> input ('edit_body');
            $ delete_flag = $ request-> input ('edit_delete_flag');
            $ edit_data = User :: where ('id', $ edit_id)-> first ();
            $ edit = array (
                'id' => $ edit_id,
                'name' => $ name,
                'email' => $ email,
                'works_id' => $ body,
                'created_at' => $ edit_data ['created_at'],
                'updated_at' => $ edit_data ['updated_at'],
                'delete_flag' => $ edit_delete_flag
            );
            $ back_url = "location.href ='/ home'";
            return view ('edit', ['User' => $ edit,'Back' => $ back_url])
                -> withErrors ($ validator);
            // Processing at the time of error
        }
        // Processing when validation passes
    }
}

It’s like the processing of one controller, but you can also write it like this. By writing with view () specified, it is possible to change pages while retaining information other than the GET method.

Retention of input value in case of error

I introduced how to hold the page transition and the value by other than redirect (), but how should the value be held in the case of redirect ()? You can use withInput (), but you can use ** old helper ** in Laravel. To use it, just enter ** {{old ('name attribute')}} ** in value such as<Input>in ~ .blade.php.

You can also set the initial value for the old helper. If you want to set the database value as the initial value, set that value in the second argument.

<td> <input type = "text" name = "edit_text" value = "{{old ('edit_text', $ db-> text)}}"> </ td>

References