使用.ini配置验证Zend_Form密码 - 可以吗?

时间:2010-09-10 21:17:33

标签: php zend-framework configuration zend-form

我开始编写一个ZF(v1.10.3)应用程序并选择使用Zend_Config_Ini来生成我的Zend_Form对象。在我必须测试相同的密码输入之前,这一切都很好。这是现在行为不端的部分:

elements.password.type = password

elements.password2.type = password
elements.password2.options.validators.identical.validator = "Identical"
elements.password2.options.validators.identical.options.token = password

它不是比较这两个元素的值,而是将password2的值与文字字符串“password”进行比较。因此,除“密码”之外的任何密码都会给我以下验证错误:

The token 'password' does not match the given token '*******'

有没有正确的方法呢?我通过Google找到Zend_Validate_Identical与Zend_Config_Ini的唯一例子来自德国网站,有人似乎推荐与我上面的失败代码完全相同的“解决方案”。

我知道在PHP代码中有很多方法可以做到这一点,但此时我已经非常重视INI配置,除非我绝对必须,否则我宁愿不放弃它或做出异常。

[编辑]这是我的全新的UserForm.ini:

method = "post"
id = "newUserForm"
accept-charset = "utf-8"

elements.username.type = "text"
elements.username.options.label = "Username"
elements.username.options.required = true
elements.username.options.validators.alnum = "Alnum"
elements.username.options.validators.strlen.validator = "StringLength"
elements.username.options.validators.strlen.options.min = "3"
elements.username.options.validators.strlen.options.max = "32"

elements.email.type = "text"
elements.email.options.label = "Email address"
elements.email.options.required = true
elements.email.options.validators.email.validator = "EmailAddress"

elements.password.type = "password"
elements.password.options.label = "Password"
elements.password.options.required = true
elements.password.options.validators.strlen.validator = "StringLength"
elements.password.options.validators.strlen.options.min = "6"

elements.password2.type = "password"
elements.password2.options.label = "Password (confirm)"
elements.password2.options.required = true
elements.password2.options.validators.identical.validator = "Identical"
elements.password2.options.validators.identical.options.token = password

elements.submit.type = "submit"
elements.submit.options.label = "Submit"

这是我的控制器动作:

public function indexAction()
{
    $formConfig = new Zend_Config_Ini(APPLICATION_PATH.'/configs/newUserForm.ini');
    $newUserForm = new Zend_Form($formConfig);
    $request = $this->getRequest();
    if ($request->isPost()) {
        if ($newUserForm->isValid($request->getPost())) {
            // create new user here
            $this->_helper->redirector('index', 'index');
        }
    }
    $this->view->newUserForm = $newUserForm;
}

1 个答案:

答案 0 :(得分:3)

我只是尝试使用与您的代码类似的东西,它似乎工作正常。我正在使用ZF 1.10.6。因此,要么在这两个版本之间修复了一个问题(但我在bug跟踪器中看不到任何相似内容),或者代码中还有其他东西导致问题。你可以发布完整的ini文件和表单代码吗?

这是我的极简主义测试脚本,供参考。也许你可以看看这是否适合你:

<?php
$view = new Zend_View();

$config = new Zend_Config_Ini('formtest.ini');
$form = new Zend_Form($config->user->signup);
$form->setView($view);

if (!empty($_POST)) {
    var_dump($form->isValid($_POST));
    var_dump($form->getValues());
}

echo $form;

formtest.ini:

user.signup.action = ""
user.signup.method = "post"
user.signup.elements.password.type = password
user.signup.elements.password2.type = password
user.signup.elements.password2.options.validators.identical.validator = "Identical"
user.signup.elements.password2.options.validators.identical.options.token = password
user.signup.elements.submit.type = "submit"

在此测试中,仅当password2为空或两个字段匹配时,表单才会生效。如果它们不匹配,我得到的错误是“两个给定的令牌不匹配”,这与你的不匹配。不确定这是否相关。