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 *

    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

    Easy way to Host your Laravel Application on Shared Hosting

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Easy way to Host your Laravel Application on Shared Hosting [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi , Folks have you finished building that your awesome project but hosting it o shared hosting is what remains, don't worry you are on the right track, I have the best way to do that.StepsCreate Database on your online cpanelUpload your project or use git to do thatSet everything up on your ENVnow we need .htaccess that you will create inside your laravel root folderInside the .htaccess add this code RewriteEngine On #ends RewriteCond %{HTTP_HOST} ^(www.)?mydomain...
    Continue Reading

    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js) [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)This article was written by Okonkwo Buchi Flair a ccolleague of mind, read and know how to simply upload your files via Amazon S3.original article can be found hereBefore I begin I would love to point out that is my first post so please pardon me any errors you may find as you read alongRecently I’ve been working on a e-learning platform which of course was going to deal with a...
    Continue Reading

    Latest