Symfony和Propel:生成动态表单

时间:2012-12-12 14:23:47

标签: propel symfony-2.1

我正在使用Symfony-2.1和Propel处理基本的CRUD应用程序。理想情况下,我的模型将在推进XML模式中定义,GUI将自动更新以反映更改。在我给定的时间限制内,我不认为这是可能的,但我想尽可能接近。

我的第一个妥协是手工创建表单类型,因为推进形式类型生成器相当简陋。但要保持手动采用的水平(例如添加新的模型类型),我想完全通过定义自定义表单类型来解决以下问题:

我有一个中等复杂的Title模型,它与TitleFragment模型有一对多的关系(代表历史出版物的主要,子标题和短标题)。 在我的HTML表单中编辑标题,我实现了标准的“添加一个”行为,以允许添加另一个TitleFragment(如所描述的那样,即symfony cookbook)。

cookbook解决方案提出了表单元素的列表结构,我添加了两个JavaScript指示符类来触发该表单元素上的jQuery控件元素生成代码:

<form [...]>
 <ol class="collection-editor collection-sortable" 
      data-prototype="{{ form_widget(form.titleFragments.vars.prototype)|e }}">
    {% for titleFragment in form.titleFragments %}
        <li>
        {{ form_widget(titleFragment) }}
        </li>
    {% endfor %}
  </ol>
  [...]
</form>

我想通过使用symfony形式的自定义表单类型的主题技术自动生成该标记。为了实现这一点,我继承了symfony集合表单类型,只是更改了它的名称,使我的自定义表单主题适用于该表单:

class SortableCollectionType extends \Symfony\Component\Form\Extension\Core\Type\CollectionType {
    public function getName() {
        return 'sortableCollection'; 
    }
}

现在,我必须通过sortableCollection_widget块来扩充默认的symfony表单主题(form_div_layout.html.twig),以强制将我的集合呈现为列表,并将一些JavaScript指示器类附加到列表中(如代码示例#1中所示) )。

问题在于,在模板中,form.titleFragments依赖于模型(并且将始终依赖于模型,因为数据需要绑定到Title模型的TitleFragments的基础集合)。

class TitleType extends BaseAbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('titleFragments', // no choice for a generic name here

      new Form\DerivedType\SortableCollectionType(), array( 
        'type' => new TitlefragmentType(),
        'allow_add' => true,
        'by_reference' => false,
    ));
}
}

我考虑过将属性的名称(例如'titleFragments')传递给模板,因为从版本1.2开始,twig支持动态访问对象属性(使用属性函数)。

有人知道如何从我的自定义表单类型到twig模板获取数据吗?那将是o.k.虽然它当然是多余的而且有点笨拙。

    $builder->add('titleFragments', 
      new Form\DerivedType\SortableCollectionType(), array( 
        'type' => new TitlefragmentType(),
        'options' => array('collectionPropertyName' => 'titleFragments'),

我没有发现form builder的Symfony API特别有帮助。

1 个答案:

答案 0 :(得分:0)

要将数据传递给视图,可以使用表单类型的finishView方法。

public function finishView(FormView $view, FormInterface $form, array $options){
        $view->vars['modelClass'] = $this->modelClass;
}

由于data_class是在表单类型上定义的,我甚至可以自动计算这个值。如果集合包含嵌套类型,我还会自动覆盖原型元素id占位符(prototypeName)。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    // extract unqualified class name and pass it to the view
    // this allows more accurate control elements (instead of "add component",
    // one can use "add modelClass")
    $dataClassStr = $options['type']->getOption('data_class');
    $parts = explode('\\', $dataClassStr);
    $this->modelClass = array_pop($parts);

    $prototypeName = '__' . $this->modelClass . 'ID__';

    if ($options['allow_add'] && $options['prototype']) {
        $prototype = $builder->create($prototypeName, $options['type'], array_replace(array(
            'label' => $options['prototype_name'] . 'label__',
        ), $options['options']));
        $builder->setAttribute('prototype', $prototype->getForm());
    }
}

在twig模板中,传递给视图的数据可通过form.vars变量获得。

{% block sortableCollection_widget %}
{% spaceless %}

    {# [...] code copied from the default collection widget #}

    <a href="#" class="sortableCollectionWidget add-entity">
        {{ form.vars.modelClass|trans }} add one
    </a>

    {# For the javascript to have access to the translated modelClass name.
       The up and down sortable controls need this to be more expressive.   #}

    <input type="hidden" name="modelClassName" value="{{ form.vars.modelClass }}"/>

{% endspaceless %}
{% endblock %}