Laravel 5.2 new Feature series--table singular group input verification function realization

Source: Internet
Author: User
Laravel 5.2 New Table singular group input verification, sounds very ignorant? Let's demonstrate this feature in a concrete example.

The corresponding route is defined first in routes.php:

Route::get (' form ', ' testcontroller@form '); Route::p ost (' Form/validate ', ' testcontroller@validate ');

Then use the artisan command to create the corresponding controller:

PHP Artisan Make:controller TestController

Edit the generated testcontroller.php file with the following contents:

     

Next create the Form view file resources/views/test/form.blade.php, and edit the contents of the form.blade.php file as follows:

@extends (' Layouts.app ') @section (' content ')                        @if (count ($errors) > 0)                            
 
   
   
    @foreach ($errors->all ( ) as $error)
  • {{$error}}
  • @endforeach
@endif @endsection

We defined the array input fields Employee[][name] and Employee[][title] in the form to enter multiple employee names and positions at the same time.

Also, notice that we injected the Formrequest request class into the Validate method of the controller TestController, we will implement the form input validation logic in this class, and we use the Artisan command to generate this request class:

PHP Artisan make:request formrequest

This generates formrequest.php in the App/http/requests directory, and the contents of the file are edited as follows:

 
   ' required|max:100 ',            ' employee.*.name ' = ' required|max:100 ',            ' employee.*.title ' = ' Required|max ' : [        ];    }}

At this point, we have completed the code writing work, we will test the array input validation, in the browser to access Http://laravel.app/form, the page is displayed as follows:

We then enter some form data:

Finally, click on the "Submit" button to display the validation error message:

  • 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.