Custom Validation Rule — Laravel

Vipul Basapati
3 min readNov 30, 2017

--

Laravel is one framework that makes validation very easy. There are many validation rules predefined, for e.g.,

  1. email,
  2. url,
  3. min,
  4. max,
  5. date etc. etc.

But sometimes those are also not enough for our needs. So, for that laravel gives us the power to create a custom validation rule of our own.

Custom Validation Rule :

By creating a custom validation rule, we can use it just the way we use it the predefined rules.

Note: With great power comes great responsibility. :)

Suppose we want to validate email field in our form, we can do this by their predefined rule, like this :

/**
* Validate the request
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->validate($request, [
'email' => 'required|email|unique:users'
]);
//Rest of the function
}

By using this, we can validate the email to be “not empty, in the email format and also unique in the users table”. Isn’t that amazing, eh?

Sure it is, and it would be more amazing if I could create one of my own. There is just one rule to it.

You have to define the rule in one of the ServiceProvider’s boot method.

Creating Custom Validation Rule :

The first thing we need to do, is create a Service Provider in the Providers directory inside app directory of your laravel installation. I think creating a separate file for all our custom validations would be more cleaner. So first we will create a Service Provider Class by using the artisan command (easy :) ).

php artisan make:provider CustomValidationServiceProvider

Above command will create a Service Provider class namely CustomValidationServiceProvider.php. We got the boilerplate of the class with the command, so all we gotta do now, is start creating our rules.

So, let’s do this.

Let’s have a problem and we solve it. Suppose you have a form and you gave the user a text field and asked them to provide a string and you check if that string is a palindrome or not.

I have a post on this palindrome problem : check it out! (It’s made in javascript, but you’ll get the idea.)

So, we can create a rule like this.

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('palindrome', function ($attribute, $value, $parameters, $validator) {
if ($value == strrev($value))
return true;
else
return false;
});
}

So, the above logic will check if the string and it’s reverse are same or not.

public function checkForPalindrome(Request $request)
{
$this->validate($request, [
'str_text' => 'palindrome'
]);
return "Your string is a valid palindrome";
}

Now, if the rule pass it will return the “Valid” and if it doesn’t then we will need to show the custom message as well for this validation. Just need to define a replacer just after the custom validation rule.

Validator::replacer('palindrome', function($message, $attribute, $rule, $parameters) {
return str_replace($message, "The string you provided is not a palindrome! Try again", $message);
});

That’s how we can create a custom validation rule in laravel 5.4. Thanks for reading. See you in the next article.

Laravel And JS Resources

Below are some resources for Laravel and Javascript and my favorite JS framework: React.

Laravel

  1. Official Docs are the best maintained for Laravel. And Laracasts is the best.

Javascript

  1. For learning Javascript, Wes Bos is the best one to teach, because I have taken at least 6 courses from him. And have everything positive. You can learn the basics of JS and get your hands-on Vanilla Javascript using JavaScript30 course.
  2. You can learn the new syntax or the modern Javascript syntax called ES6 from es6.io. (Paid)
  3. You can learn React from scratch by learning from this tutorial on ReactForBeginners.com. (Paid)

--

--