Laravel Form Validation

Source: Internet
Author: User

Create a request validation file

PHP artisan make:request articlesrequest     request created successfully.

The contents of the file are as follows:

app/http/requests/articlesrequest.php<? phpnamespace app\http\requests;use App\http\requests\request;class Articlesrequest extends request{    /**     * Determine if the user is authorized to make this Request.     *     * @return BOOL     *    /Public Function authorize ()    {        return true;   This is the administrative permission, temporarily set to True, all permissions are available    }    /**     * Get The validation rules that apply to the request.     *     * @return Array     *    /Public Function rules ()    {        return [            ' title ' = ' Required ',  Here is the specified validation of the project, and the validation of the rules, in an array of ways to write            ' content ' = ' required '        ;    }}

Validation rules Reference official website: https://laravel.com/docs/5.2/validation

Modify the controller's Store method

App/http/requests/articlesrequest.phpuse app\http\requests\articlesrequest;//public    function store (Request $ Requests) {Public    function store (articlesrequest $requests) {//Here is an instance of the articlesrequest you just created        articles:: Create ($requests->all ());        Return redirect ('/articles ');    }

Because the validation is now added, the request for entry to the store method is filtered, but the direct filter does not know the success or failure, so add an extra hint

Resources/views/articles/create.blade.php@extends (' Layout.app ') @section (' content ')

Create an article

{!! Form::open ([' url ' = '/articles/store ')]! {!! Form::label (' title ', ' title: ')!! {!! Form::text (' title ', null, [' class ' = ' Form-control '])! {!! Form::label (' content ', ' content: ')!! {!! Form::textarea (' content ', NULL, [' class ' = ' Form-control '])! {!! Form::label (' Publish_at ', ' publish_at: ')!! {!! Form::d ate (' publish_at ', date (' y-m-d '), [' class ' = ' Form-control '])! {!! Form::submit (' published article ', [' class ' = ' btn btn-primary form-control '])! {!! Form::close ()!!} @if ($errors->any ())//Add here, $errors variable is provided by Laravel, which is the variable that captures these errors and is an array
      @foreach ($errors->all () as $error)//So a loop variable array is required to get the final error message
    • {{$error}}
    • @endforeach
@endif @stop

It should be noted that in Laravel 5.2, $errors variables need to be in the middleware web to exist, if not, Laravel will error, Undefined variable:errors.

Normally, an error pop-up prompt that triggers a validation rule is displayed in the Web page, for example

The title field is required. The content field is required.

Rewrite the error message, for example, to the Chinese hint, you need to rewrite the messages method in the request file, the file is not in the default, so we need to add

app/http/requests/articlesrequest.php Public    function messages ()    {        return [            ' title.required ' =] ' This title is required! ',//For example this will match the title required rule and then trigger your hint            ' body.required ' = '  This body is a must! ', '        ;    }
  • Related Article

    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.