One, automatic verification

Automatic validation: There is some form validation on the front end (via JavaScript), but JavaScript is not easy to use in some cases (such as disabling JavaScript). Therefore, there needs to be a similar mechanism on the front end that can validate the data submitted by the user on the back end. This is called automatic validation on the back end.

1.1 Preparations

Form page routing:

Form page:

Submit route:

Controller method:

1.2 Effect Display

1.3 Convert the output effect into Chinese

1.3.1 Method 1: Custom translation

You can define automatic validation by passing a third parameter to the validate method, which is an error message. Ex. :Effect:

This approach is cumbersome, with each rule directly defining the error message.


1.3.2 Method 2: Use the language pack

Since Both Chinese and English belong to the language category, if you need to switch the prompt text, you need to have the Support of the Chinese language pack. Currently, the framework only has the EN language pack, which requires other language packs to be downloaded.

Packagist.org Composer is a leading code hosting site

Install.

Then put thevendor\laravel-lang\lang\locales\zh_CNCopy to the lang folder in resources:

Then modify the config under app.php

Change to the file name of the language pack:

Effect:

Then we dig a hole for ourselves. For example, change the name to xingming, as follows:This is not the case.Add validation to zh_CN:Effect:

In the case of form submission, if the validation fails, it will be 302 to the previous page and will bring an error to the session, which can be obtained by accessing $errors in the page. So if you use Ajax to submit to the flower and the verification does not pass the case, it will not 302 to any page, will directly return json format error message.

On the way to learning PHP, if you find this article helpful to you, then please pay attention to like comment 3 times, thank you, your must be another support of my blog.