Custom validator in Laravel 5

2019-01-08 21:23发布

I am upgrading my Laravel application from 4 to 5. However, I have a custom validator that I cannot get to work.

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

I tried doing somewhat the same in L5. I dropped a validator in app/Validators/Validators.php, and updated my composer.json.

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

However, now nothing renders on any page. What've I done wrong?

2条回答
对你真心纯属浪费
2楼-- · 2019-01-08 21:39

so here's what I did on adding a custom validation. this is for laravel 5.1

  1. run PHP Artisan make:request MyFormValidationRequest file is created under app\Requests\MyFormValidationRequest.php

Here's 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 authorize() method to true, if you're not doing any authentication. it's initial value is false. else you get a white page with a "Forbidden" error message.


  1. I added a rule under the function rules(), here's what it looks like

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

today_onwards is my new validation.

  1. I created a folder named 'Services' under App folder

  2. I created a file named 'ValidatorExtended.php' under App\Services folder , here's 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 name of the method should always start in "validate" then the name of your new validation key which should be in title case,

Another note your validation key should be separated by underscore and all small letters, in this case, "today_onwards". the underscore should be put before all first capital letters in the method name. I hope I explained it good.

TodayOnwards method is equivalent to validation name of "today_onwards",

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

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

    Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array())
    {
        return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes);
    });
    
  2. Don't forget to add below library, 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. Here's 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. That's it. done. you created your own custom validation.

  5. Additionally, 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 Request Class you use your own class which is an extension of the Request class.

查看更多
贪生不怕死
3楼-- · 2019-01-08 21:49

Try the following:

  1. Make a bind class where you can implement each rule you want extending Validator class.
  2. Make a service provider that extends ServiceProvider.
  3. Add your custom validator provider at config/app.php file.

You can create the bind at Services folder like this:

namespace MyApp\Services;

class Validator extends \Illuminate\Validation\Validator{

    public function validateFoo($attribute, $value, $parameters){  
        return $value == "foo"
    }
}

Then, use a service provider to extends the core:

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 your service provider at config/app.php like so:

'providers' => [
    ...
    ...
    'MyApp\Providers\ValidatorServiceProvider';
]
查看更多
登录 后发表回答