douliudong8108 2017-06-06 09:01
浏览 17
已采纳

在ZF2 FieldSet中至少需要一个元素

The problem

I have a Form and a FieldSet. I would like to validate that the FieldSet is not empty. Also, I want to validate each field in the FieldSet.

So far, whatever I have tried is validating one or the other, but not both. If elements is present in the Form's input filter specification, then it validates that elements is not empty, but does not validate the bar and baz fields of FieldSet. And, of course, the other way around. Any clue as to how to approach this issue would be much appreciated.

The Form

class FooForm extends Form implements InputFilterProviderInterface
{
    public function init()
    {
        $this->add([
            'name'     => 'elements',
            'type'     => Collection::class,
            'required' => true,
            'options'  => [
                'target_element' => [
                    'type' => SomeElementFieldSet::class
                ]
            ]
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            [
                'name'        => 'elements',
                'required'    => true,
                'validators'  => [
                    ['name' => 'NotEmpty']
                ]
            ]
        ];
    }
}

The FieldSet

class SomeElementFieldSet extends Fieldset implements InputFilterProviderInterface
{
    public function init()
    {
        $this->add(['name' => 'bar']);
        $this->add(['name' => 'baz']);
    }

    public function getInputFilterSpecification()
    {
        return [
            [
                'name'       => 'bar',
                'required'   => true,
                'validators' => [
                    ['name' => 'NotEmpty']
                ]
            ],
            [
                'name'       => 'baz',
                'required'   => true,
                'validators' => [
                    ['name' => 'NotEmpty']
                ]
            ]
        ];
    }
}

Edit: Added full validation spec.

  • 写回答

2条回答 默认 最新

  • douna4762 2017-06-26 09:26
    关注

    After getting some hints on Google and digging through the source code, I found a solution. Unfortunately the zend-inputfilter implementation is a little buggy and won't work nicely with getInputFilterSpecification(), but we can just construct our own InputFilter and return that directly:

    The Form

    class FooForm extends BaseForm
    {
        public function init()
        {
            $this->add([
                'name'    => 'elements',
                'type'    => Collection::class,
                'options' => [
                    'target_element' => [
                        'type' => SomeElementFieldSet::class
                    ]
                ]
            ]);
        }
    
        public function getInputFilter()
        {
            if (!$this->filter) {
                $this->filter = new InputFilter();
    
                /** @var Collection $elementsCollection */
                $elementsCollection = $this->fieldsets['elements'];
    
                /** @var SomeElementFieldSet $elementsFieldSet */
                $elementsFieldSet = $elementsCollection->getTargetElement();
    
                $collectionFilter = new CollectionInputFilter();
                $collectionFilter->setIsRequired(true);
                $collectionFilter->setInputFilter(
                    $elementsFieldSet->getInputFilterSpecification()
                );
    
                $this->filter->add($collectionFilter, 'elements');
            }
    
            return $this->filter;
        }
    }
    

    This will validate that there is at least one element in the collection. And will validate all the elements one by one by the FieldSet's specification.

    One problem persists, though. Whenever the collection is empty, the validation will return false, but will not return any messages. This is due to a bug in the zend-inputfilter component. Issues reported here and here. But that is another problem altogether.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dovhpmnm31216 2017-06-06 12:41
    关注

    Use setValidationGroup() method in the Form object by specifying an array of input fields you want to validate. Please refer to the Doc!

    You may give a try this way. Though I have added some extra fields to the form for testing purpose only.

    class FooForm extends Form implements InputFilterProviderInterface
    {
         public function __construct($name = null, $options = array())
         {
    
            parent::__construct($name, $options);
    
            $this->add(['name' => 'title']);
    
            $this->add([
                'name'     => 'elements',
                'type'     => Collection::class,
                'required' => true,
                'options'  => [
                    'target_element' => [
                        'type' => SomeElementFieldSet::class,
                    ],
                ],
            ]);
    
            $this->add([
                'type' => 'submit',
                'name' => 'submit',
                'attributes' => [
                     'value' => 'Post'
                ],
            ]);
    
            // I pointed this. Here you can specify fields to be validated
            $this->setValidationGroup([
                'title',
                'elements' => [
                    'bar',
                ],
            ]);         
         }
    
        public function getInputFilterSpecification()
        {
            return [
                [
                    'name'       => 'title',
                    'required'   => true,
                    'validators' => [
                        ['name' => 'NotEmpty']
                    ]
                ],
            ];
        }     
    }
    

    And your fieldset class should be

    class SomeElementFieldSet extends Fieldset implements InputFilterProviderInterface
    {
        public function init()
        {
            $this->add(['name' => 'bar']);
            $this->add(['name' => 'baz']);
        }
    
        public function getInputFilterSpecification()
        {
            return [
                [
                    'name'       => 'bar',
                    'required'   => true,
                    'validators' => [
                        ['name' => 'NotEmpty']
                    ]
                ],
                [
                    'name'       => 'baz',
                    'required'   => true,
                    'validators' => [
                        ['name' => 'NotEmpty']
                    ]
                ]
            ];
        }
    } 
    

    Hope this would help!

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 I350 Gigabit Network
  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥20 谁有这个东西 继续教育的
  • ¥15 怎么使请求通过cors
  • ¥15 WDM 驱动ACPI 相关疑问
  • ¥15 prism 跨窗体共享数据绑定 wpf
  • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
  • ¥15 0基础计算机毕设,应该从哪开始?
  • ¥60 使用DKT40脑图划分ROI区域