Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending Validation class
  • Hi guys, it seems that validation is a big problem for me ;). I'm trying to implement a personal validation, creating a new rule like documentation - the rule is "unique", that check if a username already exists in database. I follow the documentation: 1) I create a file at "app/classes/myvalidation.php" with this code:
    class Myvalidation {
    
        public function _validation_unique($val, $options)
        {
            list($table, $field) = explode('.', $options);
    
            $result = DB::select("LOWER (\"$field\")")
                ->where($field, '=', Str::lower($val))
                ->from($table)->execute();
    
            return ! ($result->count() > 0);
        }
    }
    

    2) Hi call it:
    $val = Validation::factory();
    $val->add_callable('myvalidation');
    

    But when I try to use it Fuel answer: "InvalidArgumentException [ Error ]: Input for add_callable is not a valid object or class." Was it my fault? Thank you!
  • Have you checked if you can reach the class using normal means? This exception means the autoloader can't find the class. The fault is probably somewhere in either the classname or path, or there's a namespace above it. The Validation class uses normal Fuel autoloading to check if the given variable is an existing class or an object, if it's not it'll output the exception you got. Thus the fault is probably somewhere in your code...
  • Ok I'll check for an error in my code. Thank you! :)
  • I have simliar problem because I need to have custom validation rules. I have tried to put the custom validation rule i Model, but that didn't work and I have also tried to go thourgh as the docs says but that gives me the same warning as the above writer.
    Is there anymore examples with custom validation anywhere? Thanks for any help!
  • You should do what the message says, and define the method as static. If you want to use a method in the current model instance, you need to pass array($object, 'method'). This means you have to switch to passing rules as an array instead of as a string.
    // orginal
    $val->add_field('name', 'Name', 'required|nomatch[Name *]');
    
    // array method
    $val->add_field('name', 'Name', array(
        'required',
        'nomatch[Name *]',
        array($modelinstance, 'methodname'),
    ));
    
  • I had this problem before too with my own validation class/rule. The example in the doc has a non-static method for the validation rule, but to make it work the validation rule method needs to be static. Should probably fix that on the example in the docs?
  • This has already been fixed, and some clarification was added. I'm trying to get the dev-docs back online (at http://fuelphp.com/dev-docs) atm...
  • Sorry Jelmer, but I have some problem with validation.
    The problem itself is that I don't understand how extend a validation class... Sorry for this, but the documentation is very poor :-( about it. I did this: 1) Create a file named app/classes/myvalidation.php 2) Put this simple code:
    public function _validation_nomatch($val, $input)
        {
         return ($val !== $input) ? true : false;
        }
    

    3) Call it with this: $val->add_field('name', 'Name', 'required|nomatch[Name *]'); What does it means? That my form has a default value of "Name *" and I want to check if input from post is different from it.
    So I created a function called nomatch and I tried to call it. But now I have some problem:
    ErrorException [ Runtime Notice ]: call_user_func_array() expects parameter 1 to be a valid callback, non-static method Myvalidation::_validation_nomatch() should not be called statically So what's my problem? Reading documentation I understood that the first param ($val) was token from input and second one was token from tha validation (nomatch[Name *], in this case I think it is "Name *" ). Can you help me? Because every article has a simple validation... :(

Howdy, Stranger!

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

In this Discussion