Connect with us

How to Customize Laravel Validator Error with Reusable Functions

kizinho

Published

on

Follow
DEVELOPER PROGRAMMING: How to Customize Laravel Validator Error with Reusable Functions [New  Developer Programming] » Naijacrawl
Photo: Naijacrawl
site

Laravel is a powerful PHP framework that helps you to build a web application quickly. Laravel is very easy to use and maintain the codebase, I will advise using the latest version of laravel when building a new project.

Read Also : How to solve This page isn’t working HTTP ERROR 500 in Laravel

Today I will show how you can customize the laravel validator error and reused it when validating your requests and return a nice error message to the user.

Example functions

use Validatorpublic function example(Request $request){

$input = $request->all();

$rules = [

'first_name' => 'required',

'username' => 'required'

];

$validator = Validator::make($input, $rules);

if (validator->fails()){

return [

'message' => $validator->errors()

];

}




}

Now you have learned how to do your own customized message in your laravel applications. You can easily turn this function into a reusable function to avoid repeating the same code when you are validating another type of incoming request.

Separating the function to a reusable function

public function validate($input, $rules){

$validator = Validator::make($input, $rules);

if (validator->fails()){

return [

'message' => $validator->errors()

];

}

}


Reusing the function inside the example function

public function example(Request $request){
$input = $request->all();
$rules = [
'first_name' => 'required',
'username' => 'required'
];
$error = $this->validate($input, $rules);
if (error){
return $error;
}

}


Cool right, writing clean code is always good, and avoid repeating the same code in your functions.


Click Here To Comment


site


kizinho

Adike Kizito is a top-rated software developer, blogger, sports, and entertainment lover. He is the CEO of these popular websites Naijacrawl.com and Mp3ager.com. He operates his software developing task through Kizzsoft.com,... Read More

Continue Reading
Click to comment

Be first to comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Mastering laravel Routes

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Mastering laravel Routes [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, folks, we have successfully mastered MVC in the last class, if you missed it go back here.We have talked about routes and where they can be found in the project, will do a quick freshened up in routes.Laravel routes simply accept a URI and a Closure, providing a very simple and expressive method of defining routes:Before we start using a route, take note of the following :Available Router Methodsget - you use get when you want to get resources from the databasep...
    Continue Reading

    How to Upgrade Laravel Application from Version 5 to latest Version 8

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to Upgrade Laravel Application from Version 5 to latest Version 8 [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, Folks, have you feels like Laravel continuous released of new versions has let you down and you can't cope with it any longer, don't worry this article is a quick guide for you, that has I upgrade naijacrawl from 5.8 to the latest 8 versions.All you need to know before upgrading your old laravel.External packages might fail to install, so isolate your packages firstInstall fresh laravel applicationThank me later.First, all you will do to be successful when upgra...
    Continue Reading

    Mastering Laravel MVC (Model View Controller) and Folder Structures

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Mastering Laravel MVC (Model View Controller) and Folder Structures [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, folks, we have successfully created the laravel project in the last class, if you missed it go back and create a new laravel project here.Before going to MVC (Model View Controller) let's serve up the project for development purposes using the following commandphp artisan serve output resultStarting Laravel development server: http://127.0.0.1:8000 incase if you want to serve your project from a different port number, use the following command belowphp artisan...
    Continue Reading

    Latest


    Download Naijacrawl App today

    Fastest way to read on the go

    Download the Naijacrawl App.