Laravel 5 Framework Learning Form Validation _php instance

Source: Internet
Author: User
Tags locale

When creating an article, if you do not input a direct submission, OK, you get an empty article, without any error prompts, this is wrong. 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 Code code as follows:

PHP Artisan make:request createarticlerequest

The generated files are 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 authenticate the user when submitting the form, and we do not need authentication to return true. Rules are our method of rule. Let's modify this method:

 The public Function authorize ()
 {
 //is modified to true to indicate that authentication is not required or that return true is authenticated
 ;
 }
 
 Public function rules ()
 {return
 [
  ' title ' => ' Required|min:3 '],
    ' body ' => ' required ',
    ' Published_at ' => ' required|date '
 ];
 }

Other constraints can be inserted into the Laravel document. The constraint above indicates that the title is to be entered, at least 3 characters, the body is necessary, the PUBLISHED_AT is required and the 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
  
  {--using the illuminate\html Open Source library that 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 without filling out the error message.

Change the message into Chinese

The error message in English is displayed, in fact Laravel take into account the internationalization of the problem, first modify the config/app.php,

Copy Code code as follows:

' Locale ' => ' en ',

Set the locale language to Chinese, and then resources/lang the following new folder en, copy resources/lang/en/validation.php file to en directory, modify:

 "Min"         => [
 "Numeric" => "The:attribute must is at Least:min.",
 "file" => "The:attribute must  B E at least:min kilobytes. ",
 " string "=>": attribute must contain at least: min character. ",
 " array "  =>" The:attribute must have at least:min items. ",
 ],
 
 " required "       =>": attribute must be filled in. ",

Others can be translated by themselves. Submit the empty form again, the error message is Chinese. And Min:3 's judgment is also at least 3 Chinese.

--

Laravel also integrates the Validate method in the controller, in other words, we do not necessarily generate the request class, which we can do directly in the controller.

To modify a controller:

 Note the namespace of the Request, and do not mistake 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 ');
  }

The results are the same, so that simple validation can be done more quickly.

The above mentioned is to share the whole content of this article, I hope to be able to master the LARAVEL5 framework to help.

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.