2013-02-20 58 views
0

我创建形式女巫集合类型和集合元素已经与数据转换女巫场ID到实体转换器:访问形式,表单生成的数据添加自定义属性

$builder->add(
     $builder->create('template', 'text', array(
      'label' => false, 
      'required' => false, 
      'attr' => array(
       'class' => 'template-selector', 
      ) 
     ))->addModelTransformer($transformer) 
    ); 

这是必要的,因为我想将此字段变为Select2(javascript lib)ajax powered元素。

问题是我需要使用数据填充Select2,为此,我只有一个合理的(用于性能)选项将表单构建器的数据作为属性传递给参数(比如说数据值),但表单构建器除非有数据我在EventListener中创建元素,但不能添加模型转换器。

我怎么能实现这样的功能?没有任何额外的查询或Ajax调用?

回答

0

我发现这样做在树枝模板的唯一方法:

{% if item.get('value').template %} 
    {% set templ_id = item.get('value').template.getId %} 
    {% set templ_text = item.get('value').template.getTitle %} 

    {{ form_row(item.template, {'attr': {'data-value' : '{id:' ~ templ_id ~ ', text:"' ~ templ_text ~ '"}'}}) }} 
{% else %} 
    {{ form_row(item.template) }} 
{% endif %} 

如果产品形式集合元素