动态更改字段集字段的必需参数 [英] Change Fieldset Fields' required parameter dynamically

查看:27
本文介绍了动态更改字段集字段的必需参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 moneyFieldset,有 2 个字段,金额和货币.

I have a moneyFieldset with 2 fields, amount and currency.

class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);
        $this->setHydrator(...);

        $this->add(array(
            'name'    => 'currency',
            'type'    => 'select',
            'options' => array(
                'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
            ),
            'attributes' => array(
                'value' => \Core\Service\Money::DEFAULT_CURRENCY,
            ),
        ));

        $this->add(array(
            'name'       => 'amount',
            'type'       => 'text',
        ));
    }
}
public function getInputFilterSpecification()
    {
        $default = [
            'amount' => [
                'required'    => false,
                'allow_empty' => true,
                'filters'     => [
                    ['name' => AmountFilter::class]
                ],
                'validators' => [
                ]
            ],
            'currency' => [
                'required'    => false,
                'allow_empty' => true,
                'filters'     => [
                    ['name' => StringToUpper::class]
                ],
                'validators' => [
                ]
            ]
        ];
        return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
    }

我在其他字段集中使用 moneyFieldset,如下所示:

I'm using moneyFieldset in my other fieldsets like this:

        // Price Field
        $this->add(array(
            'name'       => 'price',
            'type'       => 'form.fieldset.moneyFieldset',
            'attributes' => array(
                'required'    => true,
                'invalidText' => 'Please type an amount'
            ),
            'options' => array(
                ...
            ),
        ));

当我这样设置过滤器时:

When I set filter like this:

    function getInputFilterSpecification()
    {
        'price' => [
            'required'    => true,
            'allow_empty' => false,
        ],
     }

它不起作用,因为 price 有 2 个字段,所以我怎么能说 price[amount]price[curreny] 是必需的?

It's not working because price has 2 fields, so how can I say price[amount] and price[curreny] is required?

推荐答案

您可以使用以下数组结构为嵌套字段集(在表单的上下文中)提供输入规范.

You can provide the input specifications for the nested fieldset (within the context of the form) using the following array structure.

public function getInputFilterSpecification()
{
    return [
        // ...
        'price' => [
            'type' => 'Zend\InputFilter\InputFilter',
            'amount' => [
                'required' => true,
            ],
            'currency' => [
                'required' => true,
            ]
        ],
        //...
    ];
}

如果您要动态修改输入过滤器的值,可能值得考虑使用服务工厂类创建验证器,然后使用对象 API 而不是数组将其附加到表单.

If you are dynamically modifying the values of the input filter it might be worth considering creating the validator using a service factory class and then attaching it to a form using the object API rather than arrays.

这篇关于动态更改字段集字段的必需参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆