TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
ORM filters
  • Hi to everyone!

    I don't understand. When I'm the code like this:

    $data = Validation::factory($array)
                ->rule('username', 'not_empty')
                ->rule('email', 'not_empty');
    

    and try to type just spaces, it says it's ok and saves the data. Then I added filters to my ORM Model.

     public function filters()
     {
        return array(
                TRUE => array(
                    array('trim'),
                ),
            );
    }
    

    It saves the "empty" (just spaces) value to DB. What I'm doing wrong?

  • You're mixing rules() with filters() ... Create your validation rules in rules() function: http://kohanaframework.org/3.3/guide/orm/examples/simple

  • public function rules()
    {
        return array(
            'username' => array(
                array('not_empty)
            ),
            'email' => array(
                array('not_empty')
                array('email')
            )
        );
    }
    
  • When you invoke save() method, the rules() function is triggered automatically.

  • Ok, @zoltalar, but it works in other project. And what if I want to call my function in filters?

    The following is the code of my register function.

    class Model_User extends Model_Auth_User {
    
        public function filters()
        {
            return array(
                TRUE => array(
                    array('trim'),
                ),
            );
        }
    
        public function register($data)
        {
            $data = Validation::factory($data)
                ->rule('inputUsername', 'not_empty')
                ->rule('inputUsername', 'regex', array(':value', '/^[a-z_.1-9]++/iD'))
                ->rule('inputUsername', 'min_length', array(':value', 4))
            ->rule('inputUsername', 'max_length', array(':value', 32))
            ->rule('inputUsername', 'Model_User::unique_username')
                ->rule('inputEmail', 'not_empty')
                ->rule('inputEmail', 'email')
                ->rule('inputEmail', 'Model_User::unique_email')
                ->rule('inputPassword', 'not_empty')
                ->rule('inputPassword', 'min_length', array(':value', 5))
                ->rule('inputPassword', 'max_length', array(':value', 32))
                ->rule('inputPassword', 'regex', array(':value', '/^[_a-zA-Z-0-9.*~#!]++$/uD'))
                ->rule('inputPasswordConfirm', 'not_empty')
                ->rule('inputPasswordConfirm', 'matches', array(':validation', ':field', 'inputPassword'))
                ->rule('inputFirstName', 'not_empty')
                ->rule('inputSecondName', 'not_empty')
                ->rule('checkboxTerms', 'not_empty');
    
            if (!$data->check())
            {
                return  $data->errors('User/User');
            }
    
            $this->username = strip_tags($data['inputUsername']);
            $this->email    = strip_tags($data['inputEmail']);
            $this->password = strip_tags($data['inputPassword']);
            $this->firstName = strip_tags($data['inputFirstName']);
            $this->secondName = strip_tags($data['inputSecondName']);
            $this->subscribe = (isset($data['checkboxSubscribe'])) ? 1 : 0;
    
            $this->save();
    
            $this->add('roles', ORM::factory('Role', array('name' => 'login')));
        }
    
  • If you want to call custom functions in filters() function you can do this like this:

    public function filters()
    {
        return array(
            'first_name' => array(
                array('Text::custom_function', array(':value'))
            ),
            'last_name' => array(
                array('CustomClass::another_custom_function', array(':value'))
            ),
            'password' => array(
                array('Model_Abstract::hash', array(':value'))
            )
        );
    }
    
  • @zoltalar, what does your Model_User::unique_username look like? Is it akin to Model_Member::username_available in the ORM example?

    That rule callback in the example returns TRUE when a given username already exists, and so the rule passes — but that seems backwards to me. Shouldn't it return FALSE when it exists, so the rule fails?

  • Sorry @zoltalar, I meant of course to address that to @srs! :-)

Howdy, Stranger!

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

In this Discussion