ZF2:文件集验证不起作用

ZF2 : Filedset validation is not working

我的 ZF2 表单字段集验证不起作用,但表单验证有效我已经尝试了相同的表单验证,但这些都不起作用。

我已经在字段集中实现了 InputFilterProviderInterface,但它不起作用。

下面是我的代码:

class CompanyCreditLimitFieldset extends Fieldset implements InputFilterProviderInterface {

    protected $_curency = null;
    protected $inputFilter;

    public function __construct($name = null, $options = array()) {
        $this
                ->setHydrator(new ClassMethodsHydrator(false))
                ->setObject(new \Application\Entity\PhvCompanyCurrencyCredit())
        ;

        parent::__construct($name, $options);



        $this->add(array(
            'name' => 'credit_limit',
            'type' => 'text',
            'attributes' => array(
                'id' => 'credit_limit',
                'class' => 'form-control maxlength-simple credit_limit',
                'placeholder' => 'Credit Limit'
            ),
            'options' => array(
                'label' => 'Credit Limit',
            )
        ));

        $this->add(array(
            'name' => 'mininum_balance_limit',
            'type' => 'text',
            'attributes' => array(
                'id' => 'mininum_balance_limit',
                'class' => 'form-control maxlength-simple mininum_balance_limit',
                'placeholder' => 'Minimum Balance Limit'
            ),
            'options' => array(
                'label' => 'Minimum Balance Limit',
            )
        ));
    }


    public function getInputFilterSpecification() {
        return array(
            'credit_limit' => array(
                'filters' => array(
                    array('name' => 'StringTrim')
                ),
                'validators' => array(
                    array('name' => 'NotEmpty')
                )
            ),
            'mininum_balance_limit' => array(
                'filters' => array(
                    array('name' => 'StringTrim')
                ),
                'validators' => array(
                    array('name' => 'NotEmpty')
                )
            ),

        );
    }

}

表格

class AddCompanyForm extends AbstractForm implements InputFilterAwareInterface {

    protected $inputFilter;
    protected $dbAdapter;
    private $_country = null;

    public function __construct($id = null, $name = null) {
        $this->entity = new \Application\Entity\PhvCompany();
        parent::__construct($name);

        $this
                ->setHydrator(new ClassMethodsHydrator(false))
                ->setObject(new \Application\Entity\PhvCompany())
        ;

        $this->__addElements();
    }

    private function __addElements() {
        $this->setAttribute('method', 'post');

        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'creditlimitfieldset',
            'options' => array(
                'label' => 'Credit Limits',
                'count' => 3,
                'allow_add' => true,
                'should_create_template' => true,
                'template_placeholder' => '__placeholder__',
                'target_element' => array(
                'type' => 'Application\Form\Fieldset\CompanyCreditLimitFieldset',
                ),
            ),
//             'type' => 'Application\Form\Fieldset\CompanyCreditLimitFieldset',
//             'options' => array('label' => 'Credit Limits',)
        ));


        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Save',
                'class' => 'btn btn-inline btn-secondary btn-rounded'
            )
        ));




    }

    public function setDbAdapter($dbAdapter) {
        $this->dbAdapter = $dbAdapter;
    }

    public function setInputFilter(InputFilterInterface $inputFilter) {
        throw new \Exception("Not used");
    }

}

你能帮我解决这个问题吗?

坦率地说,这类问题就是为什么我总是构建一个单独的 InputFilter 层次结构来匹配 Form 结构,而不是依赖于 ZF 的 "magic" 输入过滤器构建器。这是一篇关于如何做到这一点的好文章:

以这种方式处理集合时,您需要将 CollectionInputFilter 添加到每个集合表单元素的表单输入过滤器中。 class 是完全没有记录的 IIRC,但是你 can find an example here and the class here.


我写了一个简短的脚本来重现您所看到的内容:

<?php
<<<CONFIG
packages:
    - "zendframework/zend-form: ^2.0"
    - "zendframework/zend-servicemanager: ^3.0"
    - "zendframework/zend-hydrator: ^2.0"
CONFIG;
// Run this script with Melody: http://melody.sensiolabs.org/


use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Form\Form;

class ChildFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct($name = null, $options = array())
    {
    parent::__construct($name, $options);

    $this->add(array(
        'name' => 'fieldA',
        'type' => 'text',
        'attributes' => array(
            'id' => 'fieldA',
        ),
        'options' => array(
            'label' => 'Field A',
        )
    ));
    }

    public function getInputFilterSpecification()
    {
    return array(
        'fieldA' => array(
            'required'          => true,
            'allow_empty'       => false,
            'continue_if_empty' => false,
            'filters' => array(
                array('name' => 'StringTrim')
            ),
            'validators' => array(
                array('name' => 'NotEmpty')
            )
        ),
    );
    }
}

class ParentForm extends Form
{
    public function __construct($name, array $options)
    {
    parent::__construct($name, $options);

    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'childForms',
        'options' => array(
            'label' => 'Child Forms',
            'allow_add' => true,
            'should_create_template' => true,
            'template_placeholder' => '__placeholder__',
            'target_element' => array(
                'type' => 'ChildFieldset',
            ),
        ),
    ));

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type' => 'submit',
            'value' => 'Save',
        )
    ));
    }
}

$form = new ParentForm('foo', []);

$data = [];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid');
echo "\n\n--------------------------------------\n\n";


$data = [
    'childForms' => []
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid');
echo "\n\n--------------------------------------\n\n";


$data = [
    'childForms' => [
    ['fieldA' => ''],
    ],
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid') . "\n\n";
echo "\n\n--------------------------------------\n\n";


$data = [
    'childForms' => [
    ['fieldA' => 'fff'],
    ],
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid') . "\n\n";

当我 运行 它 (PHP 7.0.9) 我得到这个:

Dataset: Array
(
)

Result: valid
(wrong)

--------------------------------------

Dataset: Array
(
    [childForms] => Array
    (
    )

)

Result: valid
(wrong)

--------------------------------------

Dataset: Array
(
    [childForms] => Array
    (
        [0] => Array
            (
                [fieldA] => 
            )

    )

)

Result: invalid
(correct!)


--------------------------------------

Dataset: Array
(
    [childForms] => Array
    (
        [0] => Array
            (
                [fieldA] => fff
            )

    )

)

Result: valid
(correct!)

仅当数据中存在完整的键层次结构时,才会触发子字段集上的输入过滤器。 本身并不是一个错误,但它不是您期望发生的事情。