2010-07-10 59 views
0

我想让Zend_Filter_Input按照简单登录表单上的要求工作。无法使Zend_Filter_Input正常工作

这里是我的代码

$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()); 

和响应此

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 
    ) 

) 

我提到的官方文档。为什么在这里说rule "username" and rule "password"

感谢

回答

1

我是谁设计的,于2007年

实施的Zend_Filter_Input每个规则由关联数组键标识的开发商。在你的情况下,你有两个规则,“用户名”和“密码”。如果您的输入未通过某些规则,则错误消息会告诉您哪些规则未得到满足。

您的规则名称也恰好对应于您正在验证的表单字段的名称。默认情况下,规则验证的字段与规则名称相同。


重新评论:您不会传递$ _POST作为要验证的数据。为回答

$input = new Zend_Filter_Input($filters,$validators); 
$input->setData($_POST): 
+0

您好,感谢:你必须这样做要么这一点:

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

要不然这一点。我现在没有代码。 (昨天正在尝试这个工作)。但是当我打印$ _POST时,即使getMissing方法显示上述数组,它也向我展示了字段和非空字段。我在这里错过了什么吗? 还有一件事是,我实际上不需要验证这两个字段的alnum,只需要检查他们是否存在。但是跳过alnum部分会导致错误,例如找不到插件'required'。我可以只使用meta命令:需要没有任何其他验证器吗? 谢谢 – naiquevin 2010-07-11 08:04:41

+0

哦。我在想getRequest() - > getParam部分默认是传递的,第三个参数是可选的。非常感谢。现在就试试这个。 – naiquevin 2010-07-11 08:14:09

+0

这是可选的。您可以传递$ _POST,$ _GET或任何其他关联数组。但你必须通过*东西*! :-) – 2010-07-11 08:35:30