簡體   English   中英

ZF2:Filedset驗證無效

[英]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");
    }

}

你能幫我解決這個問題嗎?

坦率地說,這種類型的問題是為什么我總是構建一個與Form結構匹配的獨立InputFilter層次結構,而不是依賴於ZF的“魔術”輸入過濾器構建器。 這是一篇關於如何做到這一點的精彩文章:

以這種方式處理集合時,您需要將CollectionInputFilter添加到表單的每個Collection表單元素的輸入過濾器中。 那個類完全沒有文檔IIRC,但你可以在這里找到一個例子這里 的類


我寫了一個簡短的腳本來重現你所看到的:

<?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!)

子字段集上的輸入過濾器僅在數據中存在完整的層次結構時觸發。 這不是一個bug 本身 ,但它不是你期望發生的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM