Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation callback
  • Hi, I hope you guys can help me on this, as I can't seem to find where the problem resides.
    So I'm using the base of the stationwagon script and wanted to add a check on whether a username/email is unique. So in the docs I found the following:
    // app/classes/myvalidation.php
    class Myvalidation {
    
        public function _validation_unique($val, $options)
        {
            list($table, $field) = explode('.', $options);
    
            $result = DB::select("LOWER (\"$field\")")
                ->where("$field", '=', MBSTRING ? mb_strtolower($val) : strtolower($val))
                ->from($table)->execute();
    
            if($result->count() > 0)
                return false;
            else
                return true;
        }
    
    }
    

    And these are my validation rules:
    $val = Validation::factory('signup_user');
      $val->add_callable('myvalidation');
      $val->add('username', 'Username')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20)->add_rule('unique[users.username]');
      $val->add('password', 'Password')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20);
      $val->add('email', 'Email Address')->add_rule('required')->add_rule('valid_email')->add_rule('unique[users.email]');
      $val->add('api_id', 'Api ID')->add_rule('required')->add_rule('min_length', 10)->add_rule('max_length', 20);
      $val->add('api_key', 'Api Key')->add_rule('required')->add_rule('min_length', 10)->add_rule('max_length', 20);
    
    But for some reason I get following error:
    Notice: Invalid rule "unique[users.username]" passed to Validation, not used. in APPPATH/classes/controller/users.php [25]: add_ruleNotice: Invalid rule "unique[users.email]" passed to Validation, not used. in APPPATH/classes/controller/users.php [27]: add_rule

    Any ideas? Kind regards
  • Hello,
    you've missed one line from the docs:
    after this:
    $val = Validation::factory('signup_user'); add this:
    $val->add_callable('myvalidation'); and your problem should go away good luck!
  • Isn't it already added?
  • peter vercauteren wrote on Tuesday 19th of April 2011:
    Isn't it already added?

    If you wan't add it so it would be 'auto added', you need to read out here:
    Extending Validation class
    
    There are few approaches to extend the validation class:
    1. To extend the core class like described in the Extending Core Classes
    
    There are few aproaches on extending the core clases, you'll need the first approach: And how is the core extended is described here: http://fuelphp.com/docs/general/extending_core.html If you won't be able to make it, let me know
  • I don't quite get it... I mean even though I remove the add_callable it doesnt change the error. Also I tried extending it... Does it mean I only need to add extend Validation? Like so:
    <?php
    class Myvalidation extends Validation{
    
        public function _validation_unique($val, $options)
        {
    

    Just to be sure, the myvalidation.php still needs to be set in the classes folder.
    And should the add_callable still be set before the rules after extending the Validation class?
  • peter vercauteren wrote on Tuesday 19th of April 2011:
    I don't quite get it... I mean even though I remove the add_callable it doesnt change the error. Also I tried extending it... Does it mean I only need to add extend Validation? Like so:
    <?php
    class Myvalidation extends Validation&#123;
    
        public function _validation_unique($val, $options)
        &#123;
    

    Just to be sure, the myvalidation.php still needs to be set in the classes folder.
    And should the add_callable still be set before the rules after extending the Validation class?

    Yes it should be in app/classes/myvalidation.php
    the add_callable should be called before the rules can you post the whole file at: http://scrp.at/
    also post the controller stuff so I can check.
  • app/classes/myvalidation.php
    http://scrp.at/Qu app/classes/controller/users.php
    http://scrp.at/Qv
  • I have the same problem- trying to validate unique email address. Using RC2.
  • Well it does work when you change it to
    $val->add_field('username', 'Username', 'required|min_length[3]|max_length[20]|unique[users.username]');
    

    So apparently not when using add_rule
  • I read through this before, not sure how I missed this. add_rule() doesn't do string parsing, it expect the function and its arguements as arguements - not in a string to be parsed as the add_field() method does. That has severe limitations with types (you can't pass arrays, objects, ints, doubles).
    $val->add('username', 'Username')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20)->add_rule('unique', 'users.username');
    
  • thanks for the reply :) For some reason though I can't seem to get my errors working. when I try and test the unqiueness of the username and email, it just refreshes the page and empties the form. http://scrp.at/TF
  • I have no idea what your view looks like, so I can't tell you why that is.
  • Oh sorry, well this is the view: http://scrp.at/TM and the template controller view:
    http://scrp.at/TN
  • Hello, to refill you from, you need smth like this, ebcause you don't tell the Form class to fill out the form for you,
    for example you can do: <?php echo Form::input('name', Input::post('name')); ?> This way it will load the form, but if the submit happens, it will refill the form input 'name' with the post value 'name' does it looks more clear for you now?
  • Jaroslav Petrusevic wrote on Wednesday 20th of April 2011:
    Hello, to refill you from, you need smth like this, ebcause you don't tell the Form class to fill out the form for you,
    for example you can do: <?php echo Form::input('name', Input::post('name')); ?> This way it will load the form, but if the submit happens, it will refill the form input 'name' with the post value 'name' does it looks more clear for you now?

    Thanks =)
    Do I put that where the NULL parameter is set?
  • Not sure what you mean by your last question to huglester, but maybe he does? Also you're checking for a variable $errors, but I don't see it set anywhere?
    You might want to add something like this to your controller:
    $this->template->errors = $val->show_errors();
    
  • I probably get what he mean... basicly, your current:
    <?php echo Form::input('username', NULL, array('size' => 30)); ?>
    
    would become:
    <?php echo Form::input('username',  Input::post('username'), array('size' => 30)); ?>
    

    You can also show all errors using some styling, like this: you can add these options for example to app/config.php: http://scrp.at/TQ (this adds some styling to form errors) And then you can call smth like this in the view directly:
    <?php echo Validation::instance()->show_errors(); ?>
    

    If there will be errors, it's style them and show, if no errors, it'll be null - so no output
  • K so refill of the input fields works... But I can't seem to get the errors to work.
    I tried setting them in the controller and even tried to call them directly in the view. Nothing It just refreshes.
  • Since your validations starts like this:
    $val = Validation::factory('signup_user'); Maybe try to passing the form name here:
    <?php echo Validation::instance('signup_user')->show_errors(); ?>
  • WELL ... that works :D
    Sorry, most of the time it are these simple mistakes I keep making. Thanks

Howdy, Stranger!

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

In this Discussion