Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
problem with custom validation error message for
  • Hi, I have some problems with custom validation messages for the 'valid string' rule when used with parameters. Example Code as follows: <code>
    $check = Validation::factory();
    $check->set_message('valid_string', 103);
    $check->add('user_lastname')->add_rule('required')->add_rule('valid_string',array('alpha','numeric','spaces','dots'))->add_rule('max_length',255); if ($check->run($_REQUEST)) {
    echo 'ok';
    } else {
    foreach ($check->errors() as $error) {
    echo $error;
    }
    }
    </code> Used like this I get an Exception: <code>
    Notice! ErrorException [ Notice ]: Array to string conversion COREPATH/classes/validation/error.php @ line 119: 118:
    119: return $open.str_replace($find, $replace, $msg).$close;
    120: } </code> without the 'alpha' etc. - parameters everything works fine. Is there a special syntax for the error message to be followed with this kind of validators? Thanks & Best Wishes: Jan.
  • This was originally an issue I've posted on the previous bug tracker site. But somehow this get kicked out from github issues, when everything moved there.
    Don't know : my English is too bad or so :) Have a look at my original report, it can probably help :

    When the Validation class is used, let's say with something like this :
    $val = Validation::factory('login_user');
            $val->add('username', 'Username')
                ->add_rule('valid_string', array('alpha', 'numeric', 'dashes', 'dots'));
    
    the valid_string rule takes an array as argument. The problem is that the get_message() function of the Validation_Error class uses str_replace() to assign to each param a value. Which can eventually be an array (as shown in the example above). In order to reproduce this issue, you will need to use a custom error message, because with the default message, the function returns earlier (line 102). As a temporary fix, I flattened the $params array before entering the foreach loop (line 113). We can achieve that by writing something like this :
            $this->params = $this->flatten_array($this->params); //added line
            foreach($this->params as $key => $val)
           &#123;
                 $find[]           = ':param:'.($key + 1);
                 $replace[]    = $val;
            }
    
    Then we can define the flatten_array function (for instance) as :
        function flatten_array(array $array)
        &#123;
              $flat_array = array();
              foreach(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)) as $value)
              &#123;
                         $flat_array[] = $value;
              }
              return $flat_array;
          }
    
    This is a fix that implies modifying the Fuel core, which is not that great (http://scrp.at/Uu). I didn't check if the bug was fixed in the RC2 version yet. But it seems the Validation_Error class hasn't changed so much. Good luck!
  • As of posting RC3 is the most reason stable version, and this behaviour is still here. The work around works, but is ofcourse a bit nasty..
  • If it feel of the radar when we moved our issues, did anything think of just adding it again? To avoid having this kind of discussions all over the place (and waisting valuable time doing so), our point of view towards bugs is simple: if it isn't in the issue tracker, it does not exist.
  • Thanks for your feedback and your workaround code! Everything still the same with RC2. With a workaround like yours it works fine so far, but it'd be really nice not to have to modify the core classes. So maybe someone from the fuel developer team wants to have a look at this? ;) thanks again & happy easter: jan.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion