php - Laravel Validation: How to access rules of an attribute in customized validation -


in below rules, have custom validation customrule: *date*

$rules = [   'my_date' => 'required|date_format: y-m-d|customrule: somedate', ]; 

inside custom validation rules extension, need access date_format attribute of rule:

validator::extend('customrule', function($attribute, $value, $parameters) {     $format = $attribute->getrules()['date_format']; // need      return $format == 'y-m-d'; }); 

how can rule value of attribute on extended validator?

you can't access other rules. validators independent units - data should use is:

  • value of field being validated
  • values passed validation rule parameters
  • values of other attributes of object being validated

it seems need custom validator wrap date_format , customrule doing:

validator::extend('custom_date_format', function($attribute, $value, $parameters) {   $format = $parameters[0];   $somedate = $parameters[1];    $validator = validator::make(['value' => $value], ['value' => 'date_format:' . $format]);    //validate dateformat   if ($validator->fails()) {     return false;   }    //validate custom rule using $format , $somedate , return true if passes }); 

once have it, can use that:

$rules = [   'my_date' => 'required|custom_date_format:y-m-d,somedate', ]; 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -