Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Zend Form 2生成嵌套字段集?_Php_Zend Framework2_Zend Form2_Zend Form Fieldset - Fatal编程技术网

Php 如何使用Zend Form 2生成嵌套字段集?

Php 如何使用Zend Form 2生成嵌套字段集?,php,zend-framework2,zend-form2,zend-form-fieldset,Php,Zend Framework2,Zend Form2,Zend Form Fieldset,你好!我想通过zend framework 2创建一个类似上图的表单。输入字段如下所示: 1. dependent_features[0], 2. dependent_features[1] etc 以及它们相应的独立特征,如: 1. independent_features[0][], 2. independent_features[1][] etc. 现在,如何使用集合输入字段及其验证生成此类表单?请参见 首先,让Zend表单定义HTML。而不是相反 例如,对于您的案例,您可以定义3个类

你好!我想通过zend framework 2创建一个类似上图的表单。输入字段如下所示:

1. dependent_features[0], 2. dependent_features[1] etc
以及它们相应的独立特征,如:

1. independent_features[0][], 2. independent_features[1][] etc.
现在,如何使用集合输入字段及其验证生成此类表单?

请参见

首先,让Zend表单定义HTML。而不是相反

例如,对于您的案例,您可以定义3个类:ParentContainer,它包含DependentFeature字段集的集合。每个DependentFeature字段集都包含Select元素和一组独立的Feature字段集。每个独立的Feature FeelSet都包含一个选择元素

生成的HTML如下所示:

dependentFeatures[0][dependentFeature]
dependentFeatures[0][independentFeatures][0][independentFeature]
dependentFeatures[0][independentFeatures][1][independentFeature]
dependentFeatures[0][independentFeatures][2][independentFeature]

dependentFeatures[1][dependentFeature]
dependentFeatures[1][independentFeatures][0][independentFeature]
dependentFeatures[1][independentFeatures][1][independentFeature]
dependentFeatures[1][independentFeatures][2][independentFeature]
/**
 * @Annotation\Name("parentContainer")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 */
class ParentContainer
{

    /**
     * @Annotation\ComposedObject({
     * "target_object":"DependentFeature",
     * "is_collection":"true",
     * "options":{"count":2}
     * });
     *
     * @var DependentFeature[]
     */
    private $dependentFeatures;
}

/**
 * @Annotation\Name("dependentFeature")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 * @Annotation\Instance("DependentFeature")
 * @Annotation\Type("Zend\Form\Fieldset")
 */
class DependentFeature
{

    /**
     * @Annotation\Type("Zend\Form\Element\Select")
     * @Annotation\Options({"label":"Dependent Feature:"})
     */
    private $dependentFeature;

    /**
     *
     * @Annotation\ComposedObject({
     * "target_object":"IndependentFeature",
     * "is_collection":"true",
     * "options":{"count":3}
     * });
     *
     * @var IndependentFeature[]
     */
    private $independentFeatures;
}

/**
 * *
 * @Annotation\Name("independentFeature")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 * @Annotation\Instance("IndependentFeature")
 * @Annotation\Type("Zend\Form\Fieldset")
 */
class IndependentFeature
{

    /**
     * @Annotation\Type("Zend\Form\Element\Number")
     * @Annotation\Options({"label":"Independent Feature:"})
     */
    private $independentFeature;
}
换句话说,定义元素集合,嵌套它们,让Zend为您完成HTML工作

使用Zend表单注释,您可以执行以下操作:

dependentFeatures[0][dependentFeature]
dependentFeatures[0][independentFeatures][0][independentFeature]
dependentFeatures[0][independentFeatures][1][independentFeature]
dependentFeatures[0][independentFeatures][2][independentFeature]

dependentFeatures[1][dependentFeature]
dependentFeatures[1][independentFeatures][0][independentFeature]
dependentFeatures[1][independentFeatures][1][independentFeature]
dependentFeatures[1][independentFeatures][2][independentFeature]
/**
 * @Annotation\Name("parentContainer")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 */
class ParentContainer
{

    /**
     * @Annotation\ComposedObject({
     * "target_object":"DependentFeature",
     * "is_collection":"true",
     * "options":{"count":2}
     * });
     *
     * @var DependentFeature[]
     */
    private $dependentFeatures;
}

/**
 * @Annotation\Name("dependentFeature")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 * @Annotation\Instance("DependentFeature")
 * @Annotation\Type("Zend\Form\Fieldset")
 */
class DependentFeature
{

    /**
     * @Annotation\Type("Zend\Form\Element\Select")
     * @Annotation\Options({"label":"Dependent Feature:"})
     */
    private $dependentFeature;

    /**
     *
     * @Annotation\ComposedObject({
     * "target_object":"IndependentFeature",
     * "is_collection":"true",
     * "options":{"count":3}
     * });
     *
     * @var IndependentFeature[]
     */
    private $independentFeatures;
}

/**
 * *
 * @Annotation\Name("independentFeature")
 * @Annotation\Hydrator({"type":"Zend\Hydrator\ClassMethods", "options": {"underscoreSeparatedKeys": false}})
 * @Annotation\Instance("IndependentFeature")
 * @Annotation\Type("Zend\Form\Fieldset")
 */
class IndependentFeature
{

    /**
     * @Annotation\Type("Zend\Form\Element\Number")
     * @Annotation\Options({"label":"Independent Feature:"})
     */
    private $independentFeature;
}
按如下方式创建表单:

$form = (new AnnotationBuilder())->createForm(ParentContainer::class);
如果您不使用注释,您仍然可以按照我上面提到的->通过将字段集集合嵌套到字段集集合中来构建它


元素的命名和嵌套方式取决于具体的域要求。

您尝试过什么?我们不会为您编写代码,直到问题缩小一点,我们才能提供帮助。ZF2中的字段集文档在网络上随处可见,正如我的朋友所说,您尝试了什么,但在问问题之前首先搜索了哪里对不起,但我试着通过ZF2集合生成,但什么都没有发生。字段“独立特征”直接依赖于索引相关的“依赖特征”,即“独立特征[KEY][]”直接依赖于“依赖特征[KEY]”。因此,我不知道如何通过collection元素生成这种表单。请帮帮我。我认为字段名应该是features[0][dependent]和features[0][independent][]等。你觉得怎么样?可能吧,这取决于你的具体情况。你们可以看到我在更新后的答案中做了什么。虽然我以前并没有使用过注释,但我会尝试一下!谢谢你的帮助!同样,您也可以通过编程方式构建表单。看见