Custom validator in Laravel 5

I am updating the Laravel application from 4 to 5. However, I have a special validator that I cannot get.

In L4, I created a validators.php file and included it in the global.php file using require app_path().'/validators.php'; .

I tried to do the same in L5. I removed the validator in the application /Validators/Validators.php and updated the composer.json file.

 "files": [ "app/Validators/Validators.php" ] 

However, now nothing is displayed on any page. What did I do wrong?

+7
php validation laravel laravel-5
source share
2 answers

Try the following:

  • Create a binding class in which you can implement each rule that you want to extend Validator .
  • Make a service provider that extends ServiceProvider .
  • Add your custom validator provider to the config/app.php .

You can create a binding in the Services folder as follows:

 namespace MyApp\Services; class Validator extends \Illuminate\Validation\Validator{ public function validateFoo($attribute, $value, $parameters){ return $value == "foo" } } 

Then use the service provider to extend the kernel:

 namespace MyApp\Providers; use MyApp\Services\Validator; use Illuminate\Support\ServiceProvider; class ValidatorServiceProvider extends ServiceProvider{ public function boot() { \Validator::resolver(function($translator, $data, $rules, $messages) { return new Validator($translator, $data, $rules, $messages); }); } public function register() { } } 

Finally, import the service provider into config/app.php like this:

 'providers' => [ ... ... 'MyApp\Providers\ValidatorServiceProvider'; ] 
+28
source share

here is what i did when adding custom validation. this is for laravel 5.1

  • run PHP Artisan make:request MyFormValidationRequest file app\Requests\MyFormValidationRequest.php

Here is the initial code:

 <?php namespace App\Http\Requests; use App\Http\Requests\Request; class MyFormValidationRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // ]; } } 

IMPORTANT : change the return value of the authorize() method to true if you are not authenticating. this initial value is false. otherwise, you will receive a white page with the "Forbidden" error message.


  1. I added a rule to the rules() function, here is what it looks like

     public function rules() { return [ 'activeuntil' => 'today_onwards' ]; } 

today_onwards is my new check.

  1. I created a folder called "Services" in the "Application" folder

  2. I created a file called "ValidatorExtended.php" in the App \ Services folder, here is the code below:

      <?php namespace App\Services; use Illuminate\Validation\Validator; use Carbon\Carbon; class ValidatorExtended extends Validator { private $_custom_messages = array( "today_onwards" => "The :attribute must be today onwards", ); public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) { parent::__construct( $translator, $data, $rules, $messages, $customAttributes ); $this->_set_custom_stuff(); } protected function _set_custom_stuff() { //setup our custom error messages $this->setCustomMessages( $this->_custom_messages ); } protected function validateTodayOnwards( $attribute, $value ) { $now = strtotime('-1 day'); $valueDateFormat = strtotime($value); if($valueDateFormat > $now){ return true; } else { return false; } } } 

Note. The validateTodayOnwards method is where you put your logic. the method name should always begin with "verification" and then in the name of the new verification key, which should be in the header,

One more note . Your verification key must be separated by an underscore and all lowercase letters, in this case "today_onwards". An underscore must be placed before all uppercase letters in the method name. Hope I explained it well.

The TodayOnwards method is equivalent to the verification name "today_onwards",

another example, if I created validateOldPassword, your validation key should be "old_password".

  1. I added the code below app\Providers\AppServiceProvider.php inside the boot() method.

     Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array()) { return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes); }); 
  2. Remember to add the library below, one is the Validator class and the other is your own class, which is the " ValidatorExtended ".

     use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator; 
  3. This is what the whole file looks like, [ app\Providers\AppServiceProvider.php ]

     <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array()) { return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes); }); } /** * Register any application services. * * @return void */ public function register() { // } } 
  4. What is it. made. You have created your own validation.

  5. In addition, if you want to use it in your controller, below is the code:

     class testController extends Controller { public function updatePass(MiscValidation $request){ //code here } } 

Instead of using the Request class, you use your own class, which is an extension of the Request class.

+4
source share

All Articles