Laravel 5 Framework Form validation code

Source: Internet
Author: User

Run PHP artisan at the command line to see an option make:request, create a new form request class. Executing at the command line

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 ' = ' r ' Equired|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 ()) <ul class= "alert Alert-danger" > @foreach ($errors->all () as $error) <li> {{$error}}</li> @endforeach </ul> @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 to 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,

' 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 is at Le Ast: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. ",


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 method in the controller, and we do not necessarily need to generate the request class, which can be done directly in the controller.

To modify a controller:

Note the Request namespace, and do not make a mistake about the public function store (\illuminate\http\request $request) {$this->validate ($request, [     ' Title ' = ' Required|min:3 ', ' body ' = ' required ', ' published_at ' = ' required|date '];     Article::create ($request->all ());  Return redirect (' articles '); }

Laravel 5 Framework form validation code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.