Skip to content
pakainfo

Pakainfo

Web Development & Good Online education

  • Home
  • Blog
  • Categories
  • Tools
  • Full Form
  • Guest Post
    • Guest Posting Service
  • Advertise
  • About
  • Contact Us

PHP Laravel 6 Form Validation Script

December 28, 2019 Pakainfo Technology, Laravel, Mysql, Mysqli, php, Programming Leave a comment

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

Contents

  • PHP Laravel 6 Form Validation Script
    • Step 1 Laravel 6 Set Error Messages in Controller
    • Step 2 Laravel 6.2 Adding Error Messages in Language File
    • Step 3 Creating Custom Request
    • Open the MovieFormRequest
    • Read
    • Summary
    • Related posts

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 :

  • Jobs
  • Make Money
  • Programming
Also Read This ๐Ÿ‘‰   How to display Custom Validation Error Messages in laravel

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.

Related posts:

  1. Laravel 7 Form Validation rules Tutorial Example
  2. Laravel Form request, Validations and clean controller
  3. Laravel 5.8 Form Validation Using Jquery
  4. Laravel 7 jQuery Ajax Form Validation Example
  5. Laravel Form Automatic model validation Example
  6. Simple Laravel Form Validation Example
  7. Laravel 6 After Today Validation Tutorial Example
  8. Laravel Validation and Bootstrap Modal Popup Form Submit with Ajax
  9. Stopping On First Validation Failure in Laravel Bail Rule
client side validation in laravel 6empty field validation in phpform validation laravel 6How to customize error messages in Request Validation in LAravel 6.2?laravel 5.6 custom validation error messageslaravel 6 basic crudlaravel 6 form validation and submit to databaselaravel 6 form validation client sidelaravel 6 form validation custom error messageslaravel 6 form validation examplelaravel 6 form validation example code downloadlaravel 6 image uploadlaravel 6 validationlaravel 6 validation libraryLaravel 6.2 Form Validation with Error Messages submit form in laravel 6Laravel 6.2 Validation with Custom Error Messageslaravel and vue js demolaravel api validationlaravel crud operation 6laravel custom validation message in controllerlaravel custom validation message not workinglaravel email validation regexlaravel password validationlaravel regex validationlaravel validation unique exceptphp laravel 6 form validationPHP Laravel 6.2 Set Custom Validation Error Messages Examplesimple form validation in laravel 6validation in php registration form

Post navigation

Previous Post:Laravel 6.2 Get Last Inserted ID
Next Post:Laravel 6 insert or update multiple records

Advertise With Us

Increase visibility and sales with advertising. Let us promote you online.
Click Here

Write For Us

Weโ€™re accepting well-written informative guest posts and this is a great opportunity to collaborate.
Submit a guest post to [email protected]
Contact Us

Freelance web developer

Do you want to build a modern, lightweight, responsive website quickly?
Need a Website Or Web Application Contact : [email protected]
Note: Paid Service
Contact Me

Categories

3movierulz (64) Ajax (464) AngularJS (377) ASP.NET (61) Bio (109) Bollywood (108) Codeigniter (175) CSS (98) Earn Money (93) Education (63) Entertainment (130) fullform (87) Google Adsense (64) Highcharts (77) History (40) Hollywood (109) JavaScript (1359) Jobs (42) jQuery (1423) Laravel (1088) LifeStyle (53) movierulz4 (63) Mysql (1035) Mysqli (894) php (2133) Programming (2345) Python (99) Software (178) Software (90) Stories (98) tamilrockers (104) Tamilrockers kannada (64) Tamilrockers telugu (61) Tech (147) Technology (2416) Tips and Tricks (130) Tools (214) Top10 (505) Trading (94) Trending (76) VueJs (250) Web Technology (112) webtools (200) wordpress (166) World (343)

A To Z Full Forms

Access a complete full forms list with the meaning, definition, and example of the acronym or abbreviation.
Click Here
  • Home
  • About Us
  • Terms And Conditions
  • Write For Us
  • Advertise
  • Contact Us
  • Youtube Tag Extractor
  • Info Grepper
  • Guest Posting Sites
  • Increase Domain Authority
  • Social Media Marketing
  • Freelance web developer
  • Tools
Pakainfo 9-OLD, Ganesh Sco, Kothariya Ring Road, Chokadi, Rajkot - 360002 India
E-mail : [email protected]
Pakainfo

ยฉ 2023 Pakainfo. All rights reserved.

Top
Subscribe On YouTube : Download Source Code
We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype Guest Posting Sites