Posts

Custom Validation Rules in Laravel 5.5

Custom Laravel solutions now come with custom validation rules. With the assistance of developers, you can easily create a website for your business. Knowing the rules will help you understand how to approach your development project with the Laravel framework.

Laravel, the most renowned PHP MVC framework in the industry, comes with a variety of inbuilt useful validation rules to deal with the validation of your application’s input data. However, one of the most robust features is to let you specify your own custom validation rules using the rule object. This technique comes in very handy when working with custom Laravel solutions.  Here is a short primer on the custom Laravel solutions that you can deploy, thanks to its custom validation rules.

Here are some example codes, written for an application where there was a need to verify a particular GitHub repository. The way we did this was by creating an API call straight to GitHub. Such a validation requirement is an ideal candidate for getting wrapping done within the custom validation rule. You can register your own custom validation rules using the rule object. To generate a new custom rule object, you just need to define the class using two methods (which are ‘passes’ and ‘message’). From here, you can place your class into the App\Rules namespace. As an alternative, to create the rule class, you can first run “php artisan make:rule ValidateRepository” from CLI and that will create the rule class skeleton in “app/Rules” directory. Then you can add your own lines of code.

namespace App\Rules;
use App\Source;
use Illuminate\Contracts\Validation\Rule;
class ValidateRepository implements Rule{
public $source;
public $branch;
public function __construct($source, $branch){
$this->source = $source;
$this->branch = $branch;
}
public function passes($attribute, $value){
if (! $this->source instanceof Source) {
return false;
}
return $this->source->client()->validRepository($value, $this->branch;);
}
public function message(){
return 'The given repository is invalid.';
}
}

What happens next is that the ‘passes’ method then receives the arguments ‘$attribute’ and ‘$value’ straight from Laravel Validator. Under validation, you will find the field name ‘$attribute’. At the same time, the ‘$value’ would be the field value. Such a method only requires a ‘true’ or ‘false’ return and nothing more, whether you are able to establish the validity of a given value or not. In the example mentioned, the ‘Source’ object would be an Eloquent model – one that represents source control providers such as GitHub. Meanwhile, the ‘message’ method must then return appropriate error messages that will then be used when the validation happens to fail. Using this method might require retrieving a particular string straight from your translation file.

As soon as the custom validation rule has been defined, it can then be used when a request comes into play. You will then need to assign such a rule to a particular attribute, and this can be instantiated right within the vast array of rules (and there are many). You can, for example, make use of the ‘validate’ method – something that is readily available from Laravel 5.5’s Request object.

use App\Rules\ValidateRepository;
$request->validate([
'repository' => ['required', new ValidRepository($this->source(), $request->branch)]
]);

If you want to make a custom validation rule in order to validate a particular phone extension, for example, the entire process would be narrowed down into five simple steps:

  1. Add the routes. You will have to add two routes to accomplish this, with the first being the route that will render the view and the second being in charge of handling the post request. These two routes will then be added to your routes.php file. Existing routes can be utilized while creating a validation rule.
  2. Create your controller. The next thing to do is to make a PhoneValidatorController.php file and input the necessary code, which is: app/Http/Controllers/PhoneValidatorController.php.
  3. Utilize the extend method found on the Validator facade of AppServiceProvider. The Closure custom validator here will take in four types of arguments: $attribute, $value, $parameters, and $validator. Now, you can write the code for new types of custom validation rules.
  4. Define your error message. In this step, you will have to define the custom error messages that go into your app and your newly established validation rules.
  5. Make a view file. For the last step, you will then have to create the view file so that you can check your validation rules. You can create a phonevalidation.blade.php type of file into a path like resources/view/phonevalidation.blade.php.

At the same time, you are also free to use your own custom rule from right within a usual Form Request – or even through any other location wherein, you will need to perform validation.

As you can see, this new and nifty feature makes it very easy to define a variety of custom validation rules in the least amount of time. As it is, Laravel already has an impressive set of really helpful validation rules and still gives you a generous amount of space to create and define your own in your unique application. This is one of many great features that makes programmers gravitate towards the Laravel framework for their custom solutions. Once you get the hang of it, don’t be surprised to find yourself heavily using this feature when writing your own codes for a number of projects.