Home >Backend Development >PHP Tutorial >Laravel 5 framework learning form validation, laravel framework_PHP tutorial

Laravel 5 framework learning form validation, laravel framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:50774browse

Laravel 5 framework learning form validation, laravel framework

When creating an article, if you submit it directly without entering anything, ok, you will get an empty article , there is no error message, this is wrong. When you run php artisan on the command line, you can see an option make:request to create a new form request class. Execute on the command line

Copy code The code is 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 be an authenticated user when submitting the form. We do not need authentication and return true. rules is our rules method. Let's modify this method:

 public function authorize()
 {
 //修改为 true,表示不需要认证,或者是通过认证
 return true;
 }
 
 public function rules()
 {
 return [
  'title' => 'required|min:3',
    'body' => 'required',
    'published_at' => 'required|date'
 ];
 }

For other constraints, you can check laravel’s documentation. The above constraints indicate that 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 the $errors variable to determine if we have an error and modify the view

  @if ($errors->any())
    <ul class="alert alert-danger">
      @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  @endif
  
  {{--使用我们添加的 illuminate\html 开源库--}}
  {!! 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');
  }

Submit the form again without filling in anything and you will see the error message.

Modify the prompt information to Chinese

The error message displayed is in English. In fact, laravel has taken into account the issue of internationalization and first modified config/app.php,

Copy code The code is as follows:
'locale' => 'zh',

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

 "min"         => [
 "numeric" => "The :attribute must be at least :min.",
 "file"  => "The :attribute must be at least :min kilobytes.",
 "string" => ":attribute 至少要包含 :min 字符。",
 "array"  => "The :attribute must have at least :min items.",
 ],
 
 "required"       => ":attribute 必须填写。",

Others can be translated by yourself. Submit the empty form again and the error message is in Chinese. And the judgment of min:3 is also at least 3 Chinese characters.

--

Laravel also integrates the validate method in the controller. In other words, we do not have to generate the request class. We can complete these tasks directly in the controller.

Modify controller:

 //注意 Request 的命名空间,不要弄错了
  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 simple verification can be completed faster.

The above is all the content shared with you in this article. I hope it will help you master the Laravel5 framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/980209.htmlTechArticleLaravel 5 framework learning form validation, when creating an article in the laravel framework, if you enter nothing directly Submit, ok, you get an empty article without any errors...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn