用于添加/编辑用户的表单我从服务管理器中获得并已安装过滤器,即测试密码。但是编辑用户时不需要这个密码。我可以以某种方式禁用 Controller 中的密码字段验证吗?

在模块的 getServiceConfig 函数中:

// ....
'UserCRUDFilter' => function($sm)
{
    return new \Users\Form\UserCRUDFilter();
},

'UserCRUDForm' => function($sm, $param, $param1)
{
    $form = new \Users\Form\UserCRUDForm();
    $form->setInputFilter($sm->get('UserCRUDFilter'));
    return $form;
},
// ....

在 Controller 中,我首先从服务管理器获取表单对象:
$form = $this->getServiceLocator()->get('UserCRUDForm');

然后在编辑用户且未指定密码时禁用用户密码验证和要求:
if ($user_id > 0 && $this->request->getPost('password') == '') {
      $form->.... // Someway gained access to the filter class and change the password field validation
}

在此之后,我进行了验证:
$form->isValid();

最佳答案

我找到了!

// If user is editted - clear password requirement
if ($user_id > 0) {
    $form->getInputFilter()->get('password')->setRequired(false);
    $form->getInputFilter()->get('confirm_password')->setRequired(false);
}
此行禁用输入表单字段的要求:)

关于forms - 如何在 Controller 中禁用 Zend Framework 2 中的字段过滤器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25061192/

10-16 15:11