删除验证器并在控制器中将required设置为false

时间:2012-10-12 06:51:05

标签: zend-framework zend-form

我有以下代码,用于创建密码字段。

// Element: password
$this->addElement('Password', 'password', array(
   'label' => 'Password',
   'description' => 'Passwords must be at least 6 characters long.',
   'required' => true,
   'allowEmpty' => false,
   validators' => array(
       array('NotEmpty', true),
       array('StringLength', false, array(6, 32)),
       )
  ));
$this->password->getDecorator('Description')->setOptions(array('placement' => 'APPEND'));
$this->password->getValidator('NotEmpty')->setMessage('Please enter a valid password.', 'isEmpty');

在我的控制器中,我需要删除验证器并根据某些条件使控制器“必需”为假。

例如: -

if($someCondition){
    //Set required to false and remove validator here somehow
}

有没有人知道这种情况的解决方案?

3 个答案:

答案 0 :(得分:7)

如果您已在控制器中实例化您的表单,请执行以下操作: -

$loginForm = new Application_Form_LoginForm();

然后您可以设置密码(或任何其他)元素的属性,如下所示: -

if($someCondition){
    $loginForm->Password->setRequired(false);
    $loginForm->Password->setValidators(array());
}

或者,当Zend_Form_Element::setRequired()返回Zend_Form_Element的实例时,您可以这样做: -

if($someCondition){
    $loginForm->Password->setRequired(false)->setValidators(array());
}

答案 1 :(得分:4)

显示不需要且未经过验证的密码表单元素是否有任何意义?您也可以从控制器中删除整个元素。

//in your controller
$form->removeElement('Password');

还要注意,设置元素'必需'并使用'NotEmpty'验证器有点多余,因为Zend_Form_Element使用 'NotEmpty'验证程序,用于验证isValid()中的'必需'。因此,如果您使用'NotEmpty',则无需将'Required'设置为true。

答案 2 :(得分:0)

在ZF3中: 想象一下,您有一个用于验证用户输入数据的表单类

namespace Application\Form;

use Zend\Form\Form;
use Zend\Form\Element\Text;

class UserForm extends Form
{  
    public function __construct()
    {
        parent::__construct();

        $this->addElements();
        $this->addInputFilter();
    }

    /**
     * Add elements to the form
     */
    private function addElements()
    {
        $usernameElement = new Text('username');
        $usernameElement->setAttribute('id', 'username');

        $passwordElement = new Text('password');
        $passwordElement->setAttribute('id', 'password');

        $this->add($usernameElement)
            ->add($passwordElement);
    }

    /**
     * Add filters and validators
     */
    private function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        $inputFilter->add([
            'name'       => 'username',
            'required'   => true,
            'filters'    => [
                [
                    'name' => 'StringTrim',
                ],
                [
                    'name' => 'StringToLower',
                ],
            ],
            'validators' => [
                [
                    'name'    => 'StringLength',
                    'options' => [
                        'min' => 1,
                        'max' => 255,
                    ],
                ],
                [
                    'name'    => 'Regex',
                    'options' => [
                        'pattern' => '/[a-z0-9_]+/',
                    ],
                ],
            ],
        ]);

    //  add filters and validators for other fields here..
}

/**
*  Make a set of fields required / not required
*/
    public function setFieldsRequirement(array $fieldNames, bool $isRequired = false)
    {
        foreach ($fieldNames as $fieldName) {
            $this->getInputFilter()
                ->get($fieldName)
                ->setRequired($isRequired);
        }
    }
}

在控制器中使用:

$form = new UserForm();
// get form data from POST params
$formData = $this->params()->fromPost();

$form->setData($formData);

// make username and password not required
$form->setFieldsRequirement(['username', 'password'], false);

if ($form->isValid()) {
   // from data processing...
}