Laravel 5 basics (11)-form verification

Source: Internet
Author: User
Laravel5 basics (11)-form verification when creating an article, if you submit without entering anything, OK, you get an empty article without any error prompt, this is not correct. Run phpartisan on the command line to view the make: request option and create a new formrequest class. Run phpartisanmake: requestCreateArtic Laravel 5 Basic (11)-form verification on the command line

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. Runphp artisanYou can see an optionmake:requestTo create a form request class. Execute

php artisan make:request CreateArticleRequest

The generated file is inapp/http/requestsDirectory. In the file, we can see two methods:

public function authorize(){return false;}public function rules(){return [//];}

authorizeWhether the user needs to be authenticated when submitting the form. if authentication is not required, true is returned.rulesIs 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 indicatetitleRequired. it must contain at least 3 characters,bodyIs required,published_atIt is required and a date.

In the view, we can always access$errorsVariable to determine whether there is an error, 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 and first modifiesconfig/app.php,

'locale' => 'zh',

Set the locale language to Chinese, and thenresources/langCreate a folderzh, Copyresources/lang/en/validation.phpFilezhDirectory, modify:

"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. Andmin:3Is also judged to be at least three Chinese characters.

--

Laravel is also integrated in the controllervalidateMETHOD. 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.

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.