【问题标题】:ZF3 - Set field in fieldset required depnding on formZF3 - 根据表单在字段集中设置字段
【发布时间】:2017-09-01 07:44:32
【问题描述】:

我有一个字段集,其中包含一个名为“company”的字段,默认情况下不需要该字段。现在有一个添加此字段集的表单,现在应该需要“公司”。有没有办法在 e 中做到这一点? G。表单工厂?或者我可以在绑定到表单的 inputfilter 类中重写字段集的输入过滤器吗?

感谢您的回复!

字段集

class MyFieldset extends Fieldset implements InputFilterProviderInterface {

    public function init() {
        $this->add([
            'name' => 'company',

            'options' => [
                'label' => 'Firma'
            ],

            'attributes' => [
                'id' => 'address-company'
            ]
        ]);
    }

    public function getInputFilterSpecification() {
        return [
            'company' => [
                'required' => false,

                'filters' => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                    ['name' => 'ToNull']
                ],

                'validators' => [
                    [
                        'name' => 'StringLength',

                        'options' => [
                            'encoding' => 'UTF-8',
                            'min' => 1,
                            'max' => 128
                        ]
                    ]
                ]
            ],
        ];
    }
}

表格

class MyForm extends Form {

    public function init() {
        $this->add([
            'type' => MyFieldset::class,
            'name' => 'test',

            'options' => [
                'use_as_base_fieldset' => true
            ]
        ]);
    }

}

表单工厂

class MyFormFactory implements FactoryInterface {

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $hydratorManager = $container->get('HydratorManager');
        $inputFilterManager = $container->get('InputFilterManager');

        $form = new MyForm();
        $form->setHydrator($hydratorManager->get(DoctrineObject::class));
        $form->setInputFilter($inputFilterManager->get(MyInputFilter::class));
        $form->bind(new Entity());

        return $form;
    }

}

【问题讨论】:

  • 可以在我的一个问题上查看this answer,并将其与this answer 结合起来。第一个是将您的 InputFilters 从您的 Fieldsets 中分离出来。第二个是关于创建一个附加选项,允许您将具有 Fieldset 类型的 Inputs(因此另一个 Fieldset)设置为(不是)必需的。然后覆盖默认的 isValid 方法以允许该检查。需要做一些工作,但使用 Fieldsets 和 Collections 会更容易/更干净 :) 顺便说一句,关于 ZF2 的链接,但可能会有所帮助。

标签: forms zend-framework2 zend-framework3


【解决方案1】:

我能想到的唯一方法就是。

在您的字段集中使用:

private $companyRequired = FALSE;

public function setCompanyRequired($companyRequired)
{
    $this->companyRequired = (bool) $companyRequired;
}

public function getInputFilterSpecification()
{
    return [
        'company' => [
            'required' => $this->companyRequired,
            'filters' => [
                ['name' => 'StripTags'],
                ['name' => 'StringTrim'],
                ['name' => 'ToNull']
            ],
            'validators' => [
                [
                    'name' => 'StringLength',
                    'options' => [
                        'encoding' => 'UTF-8',
                        'min' => 1,
                        'max' => 128
                    ]
                ]
            ]
        ],
    ];
}

在需要公司的表格中,在添加字段集后将其放在您的init() 方法中:

$this->get('test')->setCompanyRequired(TRUE);

在您验证表单之前,不会读取 getInputFilterSpecification() 方法,这应该可以工作。

【讨论】:

    【解决方案2】:

    我之前回答过a similar question relating to over loading the default input filter options of a nested fieldset

    您可以使用Zend\InputFilter\InputFilterProviderInterface在表单内执行相同操作

    class MyForm extends Form implements InputFilterProviderInterface
    {
    
        public function init() 
        {
            //..
        }
    
        public function getInputFilterSpecification()
        {
            return [
                'test' => [
                    'type' => 'Zend\\InputFilter\\InputFilter',
                    'company' => [
                        'required' => true,
                    ],
                ],
            ];
        }
    
    }
    

    或者,您可以通过在MyFormFactory 中手动配置字段集的输入过滤器来实现相同的目的;

    class MyFormFactory implements FactoryInterface 
    {
    
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
        {
            //.. same code
    
            $form->getInputFilter()  // fetch the input filter you just attached.
                ->get('test')        // the name of the neseted fieldset (another input filter)
                ->get('company')     // the name of the input
                ->setRequired(true); // set required to true :)
    
            return $form;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2023-04-06
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      相关资源
      最近更新 更多