ZF2 不验证集合中的元素

ZF2 dont validate element inside a collection

我在验证带有集合类型元素的表单时遇到问题,

首先,我创建了一个 "Collection" 类型元素,然后添加了几个文本类型元素。

表单呈现正确,问题是表单始终有效。

如何验证集合类型元素?

表格class:

class TestForm extends Form {

private $inputFilter;

public function __construct($name = null) {

    parent::__construct($name);

    $this->add(array(
        'name' => 'submit',
        'type' => 'Zend\Form\Element\Submit',
        'options' => array(
            'label' => 'Submit',
        ),
        'attributes' => array(
            'class' => 'form-control',
            'value' => 'submit'
        ),
    ));

    $docs = array(
        array('name' => "doc A"),
        array('name' => "doc B")
    );

    // add collection of docs.

    $collection = new \Zend\Form\Element\Collection();
    $collection->setName('docs');

    foreach ($docs as $key => $doc) {

        $element = new \Zend\Form\Element\Text($key);

        $element->setOptions(array(
            'label' => $doc['name'],
        ));

        $element->setAttributes(array(
            'class' => 'form-control input-sm',
        ));

        $collection->add($element);
    }

    $this->add($collection);
}

public function getInputFilter() {

    $this->inputFilter = new InputFilter();

    $this->inputFilter->add(array(
        'name' => "docs",
        'required' => true,
        'filters' => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
    ));

    return $this->inputFilter;
}

}

控制器class:

class IndexController extends AppController {

public function indexAction() {

    $form = new \Application\Model\Form\TestForm();

    $request = $this->getRequest();

    if ($request->isPost()) {

        $data = $this->params()->fromPost();
        $form->setData($data);
        $form->setInputFilter($form->getInputFilter());

        if ($form->isValid()) {
            pr("is valid");
        } else {
            pr($form->getMessages());
        }
    }


    return new ViewModel(array(
        'form' => $form
    ));
}

查看class:

<?php
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('docs'));
echo $this->formRow($form->get('submit'));
echo $this->form()->closeTag();
?>

您的 Collection 始终有效,因为它包含字段。你不能那样做。

您应该考虑将验证器添加到 DocADocB 字段。这将按如下方式工作以设置正确的输入过滤器:

$form->getInputFilter()->get('docs')->get('DocA')->getValidatoChain()->attachByName('YourValidatorName');

对于自定义验证器。

或:

$form->getInputFilter()->get('docs')->get('DocA')->setRequired(true);
$form->getInputFilter()->get('docs')->get('DocA')->setAllowEmpty(false);

您还可以向它们添加 Zend 验证器。

$form->getInputFilter()->get('docs')->get('DocA')->getValidatorChain()->attach(new NotEmpty([with params look docs for that])

请注意,如果您不使用 ServiceManager 检索验证器,则需要将转换器设置为选项。

不要忘记正确设置 validationGroup 或不指定要使用的 validationGroup VALIDATE_ALL。

与验证器一样,您也可以添加过滤器,如下所示:

$form->getInputFilter()->get('docs')->get('DocA')->getFilterChain()->getFilters()->insert(new StripTags())->insert(new StringTrim())