Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation with a Closure given to the add_rule
  • hi guys, i cant get Closure to throw a Validation_Error Example:
    $val->add('password', 'Password')
                    ->add_rule('required')
                    ->add_rule('min_length', 3)
                    ->add_rule('max_length', 20)
                    ->add_rule(function($val) {
                        return $val == 'test'? true:false;
                    }
                    );
    

    ErrorException [ Error ]: Cannot use object of type Closure as array COREPATH/classes/validation/error.php @ line 75
    public function __construct($field, $value, $callback, $params)
     {
      $this->field = $field;
      $this->value = $value;
      $this->params = $params;
    
      /**
       * Simplify callback for rule, class/object and method are seperated by 1 colon
       * and objects become their classname without the namespace.
       * Rules called on a callable are considered without classname & method prefix
       */
      if (is_array($callback))
      {
       foreach ($field->fieldset()->validation()->callables() as $c)
       {
        if ($c == $callback[0] && substr($callback[1], 0, 12) == '_validation_')
        {
         $callback = substr($callback[1], 12);
         break;
        }
       }
      }
      $this->callback = is_string($callback)
        ? str_replace('::', ':', $callback)
        : preg_replace('#^([a-z_]*\\\\)*#i', '', get_class($callback[0])).':'.$callback[1]; // ERROR IS IN THIS LINE
     }
    

    has anyone run into this before ? ty
  • No, but it's clearly a bug. If you make a bugreport at https://github.com/fuel/core/issues I'll take a look and fix it this weekend.
  • thx for the fast reply another problem its gonna be defining a custom error message with set_message() for closures because with dont have a string to represent the closure and assign a message.

Howdy, Stranger!

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

In this Discussion