0

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.

6
  • Exactly which field is not filtering properly? Commented Jan 30, 2010 at 21:52
  • All of them -- notice how the resulting variable types are all strings instead of being casted to the type specified in the filters (except for activated, probably because it was a null value). This is the same as just pulling the raw values out with a database query. Commented Jan 30, 2010 at 22:58
  • Are you actually setting every variable in the $data array using the __set() in your example or is some of it coming from elsewhere? Perhaps you could show some code that produces the var_dump. Commented Jan 31, 2010 at 0:17
  • Yes, I'm setting each property directly through the magic __set method, I'm positive none of the values are being set anywhere else. Commented Jan 31, 2010 at 0:22
  • As a complete side note, you are rebuilding the Zend_Filter_Input with each call to __set. You might consider building it just one time in the constructor or an init function. Sorry, I can't be of further help. Commented Feb 1, 2010 at 8:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.