views:

33

answers:

1

I'm using Zend_Filter_Input on my magic getter / setter methods to validate my input and cast fields to the type I desire. The validation portion is working great, but it's like the filters aren't triggering at all. Here is the relevant logic from my model:

    public function getFilters() {
        $filters = array(
            '*'             =>  array('StringTrim'),
            'email_opt_in'  =>  array('Boolean'),
            'admin'         =>  array('Boolean'),
            'active'        =>  array('Boolean'),
            'phone'         =>  array('Digits'),
            'activated'     =>  array('Boolean'),
            'id'            =>  array('Int'),
            'birthyear'     =>  array('Int'),
            'username'      =>  array('StringToLower')
        );
        return $filters;
    }

    public function getValidators() {
        $validators = array(
            'email'     =>  array('EmailAddress'),
            'username'  =>  array('Alnum'),
            'first'     =>  array('Alpha'),
            'last'      =>  array('Alpha'),
            'birthyear' =>  array('Digits'),
            'phone'     =>  array('Digits')
        );
        return $validators;
    }

    public function __set($name, $value) {
        if (!array_key_exists($name,$this->_data)) {
            throw new Exception('Unknown property: ' . $name);
        }
        $input = new Zend_Filter_Input($this->getFilters(), $this->getValidators(), array($name => $value));

        if ($input->isValid()) {
            if (isset($input->$name)) {
                $this->_data[$name] = $input->$name;
            } else {
                $this->_data[$name] = $value;
            }
        } else {
            throw new Exception('The following fields contain invalid values: ' . implode(',',array_keys($input->getInvalid())));
        }
    }

And yet, the output comes out like this:

object(MyApp_Model_User)#19 (1) {
  ["_data:protected"]=>
  array(15) {
    ["id"]=>
    string(1) "4"
    ["email"]=>
    string(19) "[email protected]"
    ["password"]=>
    string(32) "594851275f207072b172d7508f037d78"
    ["username"]=>
    string(6) "jdoe"
    ["first"]=>
    string(4) "Joe"
    ["last"]=>
    string(5) "Doe"
    ["phone"]=>
    string(10) "1112223333"
    ["email_opt_in"]=>
    int(1)
    ["zip"]=>
    string(5) "55555"
    ["birthyear"]=>
    string(4) "1984"
    ["gender"]=>
    string(4) "male"
    ["activated"]=>
    int(1)
    ["date_joined"]=>
    string(10) "2008-03-11"
    ["admin"]=>
    string(1) "1"
    ["active"]=>
    string(1) "1"
  }
}

Sorry for the long paste, but I feel it's relevant to understand the problem.

+1  A: 

Andy,

I have replicated this exact functionality and have realized that the Validators must be set in order for the field to be treated, although I have noticed that the Boolean Filter would return 1 as a string and not true, and null for false but I will be diving deeper at a later time. Please try to set NotEmpty validators for the rest of the fields.

AlbertRosa