0

我想在我的表单的某些字段中允许空输入。我的表单类在一个文件中,它的过滤器在另一个文件中。过滤器如下所示

class ReportFilter extends InputFilter
{
    public function __construct()
    {
        $this->add(array(
            'name'       => 'field_1',
            'required'   => false,
            'allow_empty' => true,
            'filters'    => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
        ));
    $this->add(array(
        'name'       => 'field_2',
        'required'   => false,
        'allow_empty' => true,
        'filters'    => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
    ));

问题是,即使 required 设置为 false 并且 allow_empty 设置为 true,当提交的表单在这些字段中没有任何内容时,验证失败并且它们被标记为消息“值是必需的并且不能为空”。我错过了什么?

4

2 回答 2

0

根据官方文档use validators "not_empty"

use Zend\InputFilter\Factory;

$factory = new Factory();
$inputFilter = $factory->createInputFilter(array(
    'password' => array(
        'name'       => 'password',
        'required'   => true,
        'validators' => array(
            array(
                'name' => 'not_empty',
            ),
            array(
                'name' => 'string_length',
                'options' => array(
                    'min' => 8
                ),
            ),
        ),
    ),
));

$inputFilter->setData($_POST);
echo $inputFilter->isValid() ? "Valid form" : "Invalid form";

奖金

Zend Framework 2 为您的表单提供了很多工具。

Fieldset这是和的一个很好的例子InputFilterProviderInterface。如果您使用 Doctrine,您还可以使用“Hydrator”将表单自动绑定到您的实体中。使用此代码没有问题required => false就足够了,并且允许空字符串

class AddressFieldset extends Fieldset implements InputFilterProviderInterface {

    public function __construct(ServiceManager $serviceManager) {
        parent::__construct('attribute');
        // $em = $serviceManager->get('Doctrine\ORM\EntityManager');

        // $this->setHydrator(new DoctrineHydrator($em, 'MyProject\...\Entity\AddressEntity'))->setObject(new AddressEntity());

        $this->add(array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Hidden',
            'attributes' => array(
                'autocomplete' => 'off'
            )
        ));

        $this->add(array(
            'name' => 'name',
            'options' => array(
                'label' => 'Address'
            ),
            'attributes' => array(
                'class' => 'span3',
                'autocomplete' => 'off'
            )
        ));


        $this->add(array(
            'name' => 'district',
            'options' => array(
                'label' => 'District'
            )
        ));

        $this->add(array(
            'name' => 'postal_code',
            'options' => array(
                'label' => 'Postal code'
            )
        ));

        $this->add(array(
            'name' => 'city',
            'options' => array(
                'label' => 'City'
            )
        ));
    }

    public function getInputFilterSpecification() {
        return array(
            'name' => array(
                'required' => false,
            ),
            'address' => array(
                'required' => true,
            ),
            'district' => array(
                'required' => false,
            ),
            'postal_code' => array(
                'required' => true,
            ),
            'city' => array(
                'required' => true,
            )
        );
    }
}

然后你创建一个包含你的字段集的表单

官方文档中非常好的示例,如果您控制 Form Fieldset 和 Hydrator(用于您的数据库),您将赢得很多时间。当然,在我的示例中,我指定是否需要输入,您也可以添加自定义验证器。

http://framework.zend.com/manual/current/en/modules/zend.form.collections.html

于 2016-02-24T13:04:33.300 回答
0

你试过这个符号吗?

...
'allowEmpty'=>true, //insetad of allow_empty
...

版本说明:尝试将元素声明与过滤器选项分开,如下例所示:

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;

class ReportFilter extends InputFilter
{
    public function __construct()
    {
        $this->add(array(
            'name'       => 'field_1',
            'type' => 'Zend\Form\Element\Text',
        ));        
    }
    public function getInputFilter() {
        if (!$this->filter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory ();

            $inputFilter->add($factory->createInput(array(
                'name' => 'field_1',
                'required' => false,
                'allowEmpty' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                )));
            $this->filter = $inputFilter;
        }

        return $this->filter;
    }
    public function setInputFilter(InputFilterInterface $inputFilter) {
        throw new \Exception('It is not allowed to set the input filter');
    }
}
于 2016-02-24T12:42:53.500 回答