Today, We want to share with you PHP Laravel 6 Form Validation Script.In this post we will show you Laravel 6.2 Validation with Custom Error Messages, hear for PHP Laravel 6.2 Set Custom Validation Error Messages Example we will give you demo and example for implement.In this post, we will learn about How to customize error messages in Request Validation in LAravel 6.2? with an example.
PHP Laravel 6 Form Validation Script
There are the Following The simple About laravel 6 form validation custom error messages Full Information With Example and source code.
As I will cover this Post with live Working example to develop laravel 6 form validation client side, so the simple form validation in laravel 6 is used for this example is following below.
Step 1 : Laravel 6 Set Error Messages in Controller
MovieController.php
<?php namespace App\Http\Controllers; use App\Movie; use Illuminate\Http\Request; use Validator; class MovieController extends Controller { public function store(Request $request) { // Laravel simple validator $validator = Validator::make($request->all(), [ 'name' => 'required', 'author' => 'required', ], [ 'name.required' => 'Please enter movie name', 'author.required' => 'Please enter movie author', ]); // check Laravel form validation if ($validator->fails()) { $response = [ 'success' => false, 'message' => $validator->messages() ]; return response()->json($response, 404); } // try to added the movie try { $input = $request->all(); Movie::create($input); $success = true; $message = "Stored successful"; } catch (\Illuminate\Database\QueryException $ex) { $success = false; $data = null; $message = $ex->getMessage(); } // make response $response = [ 'success' => $success, 'message' => $message ]; // return response return response()->json($response, 200); } }
Step 2 : Laravel 6.2 Adding Error Messages in Language File
resources/lang/en/validation.php
'custom' => [ 'name' => [ 'required' => 'Please enter movie name', ], 'author' => [ 'required' => 'Please enter movie author', ], ],
MovieController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Movie; use Validator; class MovieController extends Controller { public function store(Request $request) { // validator $validator = Validator::make($request->all(), [ 'name' => 'required', 'author' => 'required', ]); // check validation if ($validator->fails()) { $response = [ 'success' => false, 'message' => $validator->messages() ]; return response()->json($response, 404); } // try to store the movie try { $input = $request->all(); Movie::create($input); $success = true; $message = "Movie successfully stored"; } catch (\Illuminate\Database\QueryException $ex) { $success = false; $message = $ex->getMessage(); } // make response $response = [ 'success' => $success, 'message' => $message ]; // return response return response()->json($response, 200); } }
Step 3 : Creating Custom Request
creating a custom request using CMD
php artisan make:request MovieFormRequest
Open the MovieFormRequest
app\Http\Requests\MovieFormRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class MovieFormRequest extends FormRequest { public function authorize() { return true; } public function rules() { return [ 'name' => 'required', 'author' => 'required', ]; } public function messages() { return [ 'name.required' => 'Please enter movie name', 'author.required' => 'Please enter movie author', ]; } }
MovieController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests\MovieFormRequest; use App\Movie; class MovieController extends Controller { public function store(MovieFormRequest $request) { // try to store the movie try { $input = $request->all(); Movie::create($input); $success = true; $message = "Movie successfully stored"; } catch (\Illuminate\Database\QueryException $ex) { $success = false; $message = $ex->getMessage(); } // make response $response = [ 'success' => $success, 'message' => $message ]; // return response return response()->json($response, 200); }
Web Programming Tutorials Example with Demo
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about laravel 6 form validation and submit to database,.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโt forget to share.