How add Custom Validation Rules when using Form Request Validation in Laravel 5

前端 未结 9 1159
情歌与酒
情歌与酒 2020-12-07 12:31

I am using form request validation method for validating request in laravel 5.I would like to add my own validation rule with form request validation method.My request class

9条回答
  •  天命终不由人
    2020-12-07 13:16

    For me works the solution that give us lukasgeiter, but with a difference that we create a class with our custom validations ,like this, for laravel 5.2.* The next example is for add a validation to a range of date in where the second date has to be equals or more big that the first one

    In app/Providers create ValidatorExtended.php

     ":attribute debe ser una fecha posterior o igual a 
     :date.",
    );
    
    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 );
    }
    
    /**
     * La fecha final debe ser mayor o igual a la fecha inicial
     *
     * after_or_equal
     */
    protected function validateAfterOrEqual( $attribute, $value, $parameters, 
    $validator) {
       return strtotime($validator->getData()[$parameters[0]]) <= 
      strtotime($value);
    }
    
    }   //end of class
    

    Ok. now lets create the Service Provider. Create ValidationExtensionServiceProvider.php inside app/Providers, and we code

    app->validator->resolver( function( $translator, $data, $rules, 
      $messages = array(), $customAttributes = array() ) {
        return new ValidatorExtended( $translator, $data, $rules, $messages, 
        $customAttributes );
    } );
    }
    
    }   //end of class
    

    Now we to tell Laravel to load this Service Provider, add to providers array at the end in config/app.php and

    //Servicio para extender validaciones
    App\Providers\ValidationExtensionServiceProvider::class,
    

    now we can use this validation in our request in function rules

    public function rules()
    {
      return [
        'fDesde'     => 'date',
        'fHasta'     => 'date|after_or_equal:fDesde'
     ];
    }
    

    or in Validator:make

    $validator = Validator::make($request->all(), [
        'fDesde'     => 'date',
        'fHasta'     => 'date|after_or_equal:fDesde'
    ], $messages);
    

    you have to notice that the name of the method that makes the validation has the prefix validate and is in camel case style validateAfterOrEqual but when you use the rule of validation every capital letter is replaced with underscore and the letter in lowercase letter.

    All this I take it from https://www.sitepoint.com/data-validation-laravel-right-way-custom-validators// here explain in details. thanks to them.

提交回复
热议问题