2

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

// 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
}

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

4

3 回答 3

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());
}
于 2012-10-12T07:27:40.760 回答
4

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

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

还请注意,设置元素'Required'并使用'NotEmpty'验证器有点多余,因为Zend_Form_Element使用'NotEmpty'验证器来验证 'Required' in isValid(). 因此,如果您使用'NotEmpty',则无需将 'Required' 设置为 true 。

于 2012-10-12T09:23:13.673 回答
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...
}
于 2019-01-24T15:31:25.490 回答