Form verification for Laravel5 framework learning, laravel framework _ PHP Tutorial

Source: Internet
Author: User
Form verification for Laravel5 framework learning, laravel framework. Form verification of Laravel5 framework learning. when the laravel framework is creating an article, if you do not input anything and submit it directly, OK, you get an empty article, there is no error in form verification for Laravel 5 framework learning, laravel framework

When you create an article, if you do not input anything and submit it directly, OK, you get an empty article without any error message. this is wrong. Run php artisan on the command line to view the make: request option and create a new form request class. Execute

The code is as follows:
Php artisan make: request CreateArticleRequest

The generated 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 authenticated when submitting the form. If no authentication is required, true is returned. Rules is our rule method. Let's modify this method:

Public function authorize () {// modify to true, indicating that authentication is not required, or return true through authentication;} public function rules () {return ['title' => 'required | min: 3', 'body' => 'required', 'published _ at' => 'required | date'];}

Other constraints can be inserted into laravel's documentation. The constraints above indicate that title is required, with at least three characters, body is required, published_at is required and is a date.

In the view, we can always access the $ errors variable to determine if we have any errors and 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 and introduce our Request class.

  public function store(Requests\CreateArticleRequest $request) {    Article::create($request->all());    return redirect('articles');  }

If you submit the form again without entering anything, you can see the error message.

Change the prompt to Chinese

The error message is displayed in English. In fact, laravel considers Internationalization. first, modify config/app. php,

The code is as follows:
'Locale' => 'zh ',

Set the locale language to Chinese, and then create the folder zh under resources/lang, copy the resources/lang/en/validation. php file to the zh directory, and modify it:

"Min" => ["numeric" => "The: attribute must be at least: min. "," file "=>" The: attribute must be at least: min kilobytes. "," string "=>": attribute must contain at least min characters. "," Array "=>" The: attribute must have at least: min items. ",]," required "=>": attribute is required. ",

Others can be translated by yourself. Submit an empty form again. The error message is in Chinese. In addition, min: 3 is determined to contain at least three Chinese characters.

--

Laravel also integrates the validate method in the controller. In other words, we do not have to generate the request class, which can be done directly in the controller.

Modify controller:

// Note the Request namespace. do not mistake public function store (\ Illuminate \ Http \ Request $ request) {$ this-> validate ($ request, ['title' => 'required | min: 3', 'body' => 'requestred', 'hhed _ at' => 'required | date']); Article:: create ($ request-> all (); return redirect ('article ');}

The results are the same, so that simple verification can be completed more quickly.

The above is all the content shared in this article. I hope it will be helpful for you to master the Laravel5 framework.

Http://www.bkjia.com/PHPjc/980209.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/980209.htmlTechArticleLaravel 5 framework learning form verification, laravel framework when creating an article, if you do not input anything directly commit, OK, you get an empty article, no errors...

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.