【问题标题】:mapping the data of the child form on the parent form symfony2将子表单的数据映射到父表单 symfony2
【发布时间】:2015-02-27 07:07:02
【问题描述】:

我创建了一个为实体添加自动完成功能的表单类型,但是它需要为每个实体进行一些配置,即:我必须将配置传递给选项数组,所以我决定为每个实体创建一个新的 FormType使用我创建的 AutoCompleteType 并重用它们。但是我希望这些 Formtypes 即:每个特定实体的那些,在调用 getData() 时返回实体,现在发生的是我必须首先检索 ParentForm 的字段包含AutoCompleteType 然后调用getData() 来检索我的实体。如何将这些信息直接映射到ParentForm 上?

//the FormType of Some Entity using the AutoComplete
...
class SomeEntityAutoCompleteType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array options){
       $builder->add('some_entity', 'entity_autocomplete', array(...));
    }
}


    //the controller
    public function someAction(){
        $form = $this->get('form.factory')->create(new SomeEntityAutoCompleteType());
        ...
        //I want the below line to return my entity
        $form->getData();
        //but I have to use this one right now
        $form['some_entity']->getData()
    }

注意:我还没有实际测试过其他方法,但根据我对 Symfony 表单组件的理解,它应该是我描述的方式;

【问题讨论】:

  • 我想我必须使用 DataTransformer

标签: forms symfony


【解决方案1】:

我通过将SomeEntityAutoCompleteType 的父类型设置为我创建的主要自动完成类型并使用setDefaultOptions() 方法配置选项来解决此问题。

//SomeEntityAutoCompleteType
public function setDefaultOption(OptionsResolverInterface $resolver){
    $resolver->setDefaults(...);
}

public function getParent(){
    return "autocomplete_type";//this is the main autocomplete type I mentioned
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    相关资源
    最近更新 更多