How to perform form array validation in Laravel 5.2?

news

Laravel 5.2 gives more advanced features compared to Laravel 5.1. One of the feature included in Laravel 5.2 is “ Form Array Validation”.

#Creating blade template
We can create a simple form for using the form array validator in Laravel. Let’s imagine we want to create a form for storing user details like first name, last name etc ,so we can create a html form like,

{{ csrf_field() }}

Form Array validation

Defining The Routes
First, let’s assume we have the following routes defined in our app/Http/routes.php file:

// Display a form to create a blog post...
Route::get('form', 'PostController@create');
// Store a new blog post...
Route::post('store', 'PostController@store');

#Creating The PostController

What if, instead of an asterisk to indicate “all”, you put a specific number there? Turns out it’ll only validate the entities with that ID. So if you put person.1.name in the validation array instead of person.*.name, only the person with the ID of 1 will be validated according to those rules.

#Displaying Error Message In The Page
Add the below lines of code to displaying the error message in your page as a result of validation

@if (count($errors) > 0)
        • @foreach ($errors->all() as $error)
      • {{ $error }}

@endforeach

@endif

I hope this blog helps you in working on form array validation. Feel free to contact for any queries or you can use the comment box below to raise any suggestions or queries.

Learn More From Our Techies

Latest Post