Skip to content
Pakainfo

Pakainfo

Web Development & Good Online education

  • Home
  • Blog
  • Categories
  • Tools
  • Full Form
  • Guest Post
  • 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

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',
        ];
    }
}

Free Live Chat for Any Issue

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
Read Also:  How to Create a File Type Validation using PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Download

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.

Pakainfo
Pakainfo

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Read Also:  Bootstrap Toggle switch with ajax update to mysql in Laravel

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. PHP Laravel Validation Example Tutorial From Scratch
  8. Laravel 6 After Today Validation Tutorial Example
  9. Laravel Validation and Bootstrap Modal Popup Form Submit with Ajax
  10. Stopping On First Validation Failure in Laravel Bail Rule
Read Also:  Form validation client and server side using PHP
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

Search

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
youtube tag extractor

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 (50) Ajax (464) AngularJS (377) ASP.NET (61) Bollywood (94) Codeigniter (175) CSS (98) Earn Money (61) Education (53) Entertainment (110) fullform (79) Google Adsense (62) Highcharts (77) Hollywood (95) JavaScript (1356) Jobs (39) jQuery (1422) Laravel (1086) LifeStyle (50) movierulz4 (49) Mysql (1029) Mysqli (890) Node.js (39) php (2116) Programming (2328) Python (96) ReactJS (37) Software (114) Software (81) Stories (87) tamilrockers (90) Tamilrockers kannada (50) Tamilrockers telugu (49) Tech (120) Technology (2373) Tips and Tricks (112) Tools (138) Top10 (340) Trading (58) Trending (54) VueJs (250) Web Technology (86) webtools (153) wordpress (165) World (167)

Advertise With Us

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

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
  • YouTube Monetization
  • 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

© 2022 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 YouTube Tag Extractor