0

this line always return true even if username input is empty

if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {

my form username input looks like this

<?php
$this->addElement(new Zend_Form_Element_Text('username'));
$this->addElement('text','username',
                 array('class' => 'input-large',
                 'value' => $this->user_login,
                 'attribs'    => array('disabled' => 'disabled') /// it can be activated by button in view
));

    $username = new Zend_Form_Element_Text('username');
    $username->addValidator ( new Zend_Validate_NotEmpty() );
?>

but something like this works

$validator = new Zend_Validate_NotEmpty();
          $data = $_POST['username'];
          if($validator->isValid($data)) {

            echo 'sweet';

          }else {

            echo 'bad';
          }
4

1 回答 1

0

将元素设置为必需而不是添加验证器:

$this->addElement('text','username', array(
    'class' => 'input-large',
    'value' => $this->user_login,
    'attribs' => array('disabled' => 'disabled'),
    'required' => true
));

原因是表单元素有一个默认启用的“允许为空”标志,并且打开此标志,您的验证器将不会被使用。我在这个问题中解释了为什么会出现这种情况:Zend Framework notEmpty validator setRequired。相反,设置 required 标志会自动添加NotEmpty验证器并更改标志。

于 2013-07-10T16:09:02.693 回答