views:

31

answers:

1

Hi, I am trying to get the Zend_Filter_Input to work as required on a simple login form.

Here is my code

    $filters = array('username' => 'StringTrim', 'password'  => 'StringTrim');  
    $validators  = array( 
              'username' => array('Alnum', 'presence' => 'required'),
              'password' => array('Alnum', 'presence' => 'required')
    );

    $input = new Zend_Filter_Input($filters,$validators);

    print_r($input->getMissing());

and the response is this

Array
 (
    [username] => Array
    (
        [0] => Field 'username' is required by rule 'username', but the field is missing
    )

    [password] => Array
    (
        [0] => Field 'password' is required by rule 'password', but the field is missing
    )

  )

I am referred to the official docs. Why is it saying rule "username" and rule "password" here ?

Thanks

+1  A: 

Hi, I'm the developer who designed and implemented Zend_Filter_Input in 2007.

Each rule is identified by the associative array key. In your case you have two rules, "username" and "password". If your input does not pass some of your rules, the error messages tell you which rules were not satisfied.

Your rule names also happen to correspond to the names of the form fields you're validating. By default, the field that a rule validates is the same as the rule name.


Re your comment: You aren't passing $_POST as data to validate. You must do either this:

$input = new Zend_Filter_Input($filters,$validators,$_POST);

Or else this:

$input = new Zend_Filter_Input($filters,$validators);
$input->setData($_POST):
Bill Karwin
Hi, thanks for replying. i dont have the code right now. (was trying this at work yesterday). But when i printed $_POST, it showed me both the fields present and non empty even if the getMissing method showed the above array. Am I missing anything here ? One more thing is that i actually dont need to validate both these fields for alnum, just need to check that they are present. But skipping the alnum part gives an error, something like plugin 'required' not found. Can I just use the meta command presence:required without any other validator?thanks
naiquevin
Oh. I was thinking getRequest()->getParam part was by default passed and the third argument was optional. Thank a lot. will try this now.
naiquevin
It is optional. You can pass $_POST, $_GET, or any other associative array. But you have to pass *something*! :-)
Bill Karwin