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 *

    Laravel 8 is now released!

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Laravel 8 is now released! [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Laravel 8 is now released and includes many new features including Laravel Jetstream, a models directory, model factory classes, migration squashing, rate-limiting improvements, time testing helpers, dynamic blade components, and many more features.Before we jump into the new features, we’d like to point out that starting with version 6, Laravel now follows semver and will release a new major version every six months. You can see how the release process works here.L...
    Continue Reading

    How to fix Flutterwave common error's while Integrating payment in your Laravel App

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to fix Flutterwave common error's while Integrating payment in your Laravel App [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi folks, I was integrating FLutterwave on my laravel app, on the process i found a bunch of errors while trying to use it accept payment on my site. Have been a Paystack heavy user but decided to use Flutterwave for a reason that I love to do. Going through this article I will list the two types of error or bug that you must encounter while using Laravel Rave steps to get Flutterwave payment working.Types of errorError on the payment page when user try to refresh t...
    Continue Reading

    Latest