动态更改 Fieldset Fields 所需的参数

2024-04-11

我有一个货币字段集有 2 个字段,金额和货币。

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);
    }

我在用着货币字段集在我的其他字段集中,如下所示:

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

当我像这样设置过滤器时:

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

它不起作用,因为price有2个字段,所以我怎么说价格[数量] and 价格[货币]是必须的?


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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动态更改 Fieldset Fields 所需的参数 的相关文章

随机推荐