Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fatenfalfoul's avatar

Custom laravel rules deos not work with proengsoft / laravel-jsvalidation

Hello I'm using laravel 5.1 for our company's server requirements. I installed the"proengsoft/laravel-jsvalidation": "^1.5.0", to validate on client side my form data I'm developping a multi array form. I must validate that twice hour data are greater than other twice hour and data so let's take for example that rule:

$rules[ 'horaire.'.$i.'.heure_arrivee_lieu'] = 'checkDates:horaire'.$i.'date_arrivee_lieu, horaire'.$i.'date_depart, horaire'.$i.'heure_depart';

My custom rule on the appserviceProvider.php is:

Validator::extend('checkDates', function($attribute, $value, $parameters, $validator)
          {
            $d1 = date('Y-m-d ', strtotime($parameters[0])); 
            $h1 = date('H:i:s', strtotime($value)); 
            $d1 = date('Y-m-d H:i:s', strtotime("$d1 $h1"));

            $d2 = date('Y-m-d ', strtotime($parameters[1])); 
            $h2 = date('H:i:s', strtotime($parameters[2])); 
            $d2 = date('Y-m-d H:i:s', strtotime("$d2 $h2"));
            
              if(  $d1 <= $d2 ) 
              {
                  $validator->addReplacer('checkDates',  function ($message, $attribute, $rule, $parameters) use ($value, $validator) {
                    //return str_replace('_', ' ' , 'Le '.$attribute. ' doit être supèrieure à '.$parameters[1] );
                    
                    $other = $validator->getCustomAttributes($attribute);
                    $fields = [];
                    foreach ($other  as $parameter) {
                        $fields[] = ($parameter);
                    }
                    $i = array_search($other[$attribute], $fields); 
                    //echo '<pre>'; print_r($other[$attribute]); print_r($fields[$i-1]); exit();
                    if($i >= 1){
                        return str_replace(':attribute', $other[$attribute], (':attribute doit être supèrieur à '.$fields[$i-1]));
                    }
                    else {
                        return str_replace(':attribute', $other[$attribute], (':attribute doit être supèrieur aux horaires antécèdants'));
                    }
                    
                     
                    });  
                  return false;
              }
              return true;
          });

All is okay on the submit add form but all the custom rules deos not work on the update form what's the problem ? Thanks for help

0 likes
0 replies

Please or to participate in this conversation.