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



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

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

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

如果你在控制器中像这样实例化了你的表单:-

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

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

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

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

在 ZF3 中:假设您有一个用于验证用户输入数据的表单类

namespace ApplicationForm;
use ZendFormForm;
use ZendFormElementText;
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...
}

相关内容

最新更新