Laravel 5 Framework Learning Form validation, Laravel framework
In the creation of an article, if you do not enter a direct submission, OK, you get an empty article, without any error, this is not right. Run PHP artisan at the command line to see an option make:request, create a new form request class. Executing at the command line
Copy the Code code as follows:
PHP Artisan make:request createarticlerequest
The resulting file is in the App/http/requests directory. In the file we can see two methods:
Public function authorize () {return false;} Public Function rules () {return [ //];}
Authorize indicates whether the user needs to be an authenticated user when submitting the form, we do not need authentication and return true. Rules are our method of rule. Let's modify this method:
Public function authorize () {//modified to True, indicating that authentication is not required, or that it is passed authentication return true;} Public Function rules () {return [ ' title ' = ' Required|min:3 ', ' body ' = ' required ', ' published_at ' = > ' required|date ']; }
Other constraints can be inserted into the laravel documentation. The above constraint indicates that the title must be entered, at least 3 characters, body is required, PUBLISHED_AT is required and is a date.
In the view, we can always access $errors variables to determine if we have errors, modify the view
@if ($errors->any ())
@foreach ($errors->all () as $error)
- {{$error}}
@endforeach
@endif {--Use the illuminate\html Open Source Library We added--}} {!! Form::open ([' url ' = ' articles ')]!
Modify the controller to introduce our Request class.
Public function Store (requests\createarticlerequest $request) { article::create ($request->all ()); Return redirect (' articles '); }
Submit the form again, and you will see the error message.
Change message to Chinese
The error message is displayed in English, in fact laravel considering the internationalization of the problem, first modify the config/app.php,
Copy the Code code as follows:
' Locale ' = ' en ',
Set the locale language to Chinese, and then resources/lang the following new folder en, copy the resources/lang/en/validation.php file to the ZH directory, and modify:
"min" = = ["Numeric" = "The:attribute must is at least:min.", "file" = "The:attribute must be at Lea St:min kilobytes. "," string "=": attribute must contain at least: min character. "," array " =" The:attribute must has at least:min items. ",", " required" and "=" : attribute must be filled in. ",
Others can be translated by themselves. Submit an empty form again, the error message is Chinese. And Min:3 's judgment is at least 3 Chinese.
--
Laravel also integrates the validate approach in the controller, in other words, we don't necessarily have to generate the request class, which we can do directly in the controller.
To modify a controller:
Note the Request namespace, and don't make a mistake. Public function Store (\illuminate\http\request $request) { $this->validate ($ request, [ ' title ' = ' Required|min:3 ', ' body ' = ' required ', ' published_at ' = ' required|date ' c10/>]); Article::create ($request->all ()); Return redirect (' articles '); }
The result is the same, so that simple validation can be done more quickly.
The above is to share all the content of this article, I hope to be able to master the LARAVEL5 framework to help you.
http://www.bkjia.com/PHPjc/980209.html www.bkjia.com true http://www.bkjia.com/PHPjc/980209.html techarticle Laravel 5 Framework Learning Form validation, Laravel framework when building an article, if you do not enter a direct commit, OK, you get an empty article, no errors ...