我已经弄清楚如何使用验证和过滤器来完成这种高度动态的表单类型。我将在这里用这个假设的脚本进行解释:
// 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);
因此,您可以看到您必须为每组元素和每个字段集创建输入过滤器,然后将它们相互添加,直到构建主输入过滤器并将其分配给表单实例。
然后,这将使用提供的具有输入名称的验证器,例如general[setting_1]
在运行之后$form->setData($this->request->getPost())
-$form->isValid()
此响应可能要详细 100 倍,但它比动态字段集验证主题中可用的更好。