如何在不使用工厂的情况下验证 Zend Framework 2 中的嵌套字段集 [英] How to validate nested fieldsets in Zend Framework 2 without using factory

查看:28
本文介绍了如何在不使用工厂的情况下验证 Zend Framework 2 中的嵌套字段集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单类,它可以递归地动态创建包含元素/字段集的字段集.我这样做是为了将 settings[general][rpp][value] 作为输入名称(例如).生成这些字段是因为这些设置是用户在 XML 文件中定义的.

I've got a form class that dynamically creates fieldsets with elements/fieldsets recursively. I do this to get settings[general][rpp][value] as an input name (for example). The fields get generated because the settings are user defined in an XML file.

字段集是这样创建的:

$fieldset = new Fieldset(...);
$fieldset->add(...);
$form->add($fieldset);

表单正确输出;一切正常.除非我需要验证.

The form is being output correctly; everything works. Except i need validation.

我的目标是为这些嵌套元素定义验证器和过滤器.我真的很困惑它是如何工作的 - 但看起来只是表单本身定义了一个 input_filter setInputFilter(...) 我不知道如何让它在没有字段集的工厂和专有类,而不是动态的.

My goal is to define validators and filters for these nested elements. I'm really confused at how it works - but it looks like just the form itself defines an input_filter setInputFilter(...) and i don't know how to get it to recognize the recursion without a factory and proprietary classes for the fieldsets instead of being dynamic.

我清楚了吗?

谢谢.

推荐答案

我已经想出了如何使用验证和过滤器来处理这种高度动态的表单类型.我将在这里用这个假设的脚本来解释:

I've figured out how to do this highly dynamic type of form with validation and filters. I will explain here with this hypothetical script:

// create a form instance and a filter instance
$form = new Form();
$filter = new InputFilter();

// create a fieldset instance and another filter instance
$fieldset_a = new Fieldset('general');
$fieldset_a_filter = new InputFilter();

// create element(s) to assign to fieldset
$setting_1 = new Element('setting_1');

// create another input filter for element defining filters and validators
$setting_1_filter = new InputFilter(array(
    'name' => 'setting_1',
    'required' => true,
    'validators' => array(), // ...
));

// add element to fieldset
$fieldset_a->add($setting_1);
// add fieldset to form
$form->add($fieldset_a);

// add element filter to fieldset filter
$fieldset_a_filter->add($setting_1_filter,'setting_1');
// add fieldset A filter to main input filter
$filter->add($fieldset_a_filter,'general');

$form->setInputFilter($filter);

因此您可以看到您必须为每组元素和每个字段集创建输入过滤器,然后通过它们向后添加它们,直到构建主输入过滤器,您可以将其分配给表单实例.

So you can see you have to create input filters for each set of elements and each fieldset and then work backwards through them adding them to each other until the main input filter is built and you can assign it to the form instance.

在运行 $form->setData($this->request->getPost()) - $form->isValid()

此响应可能详细 100 倍,但它比动态字段集验证主题的可用响应要好.

This response could be 100 times more detailed, but it's better than what is available on the subject of dynamic fieldset validation.

这篇关于如何在不使用工厂的情况下验证 Zend Framework 2 中的嵌套字段集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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