我正在使用Symfony-2.1和Propel开发一个基本的CRUD应用程序。理想情况下,我的模型将在propel XML模式中定义,并且GUI将自动更新以反映更改。我认为在我给定的时间限制内,这是不可能的,但我正在尝试尽可能地接近。
我的第一个折衷方案是手动创建表单类型,因为propel表单类型生成器相当初级。但为了停留在手动采用的级别(例如,在添加新的模型类型时),我想完全通过自定义表单类型的定义来解决以下问题:
我有一个中等复杂的标题模型,它与TitleFragment模型具有一对多的关系(表示历史出版物的主标题、副标题和简短标题)。在我编辑标题的HTML表单中,我实现了标准的“添加一个”行为,以允许添加另一个TitleFragment (如在symfony 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主题(form_div_layout.html.twig),以强制将我的集合呈现为一个列表,并将一些JavaScript指示器类附加到该列表中(如代码示例#1所示)。
问题在于,在模板中,form.titleFragments依赖于模型(并且将始终依赖于模型,因为数据需要绑定到标题模型的底层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')传递给模板,因为twig从1.2版本开始就支持对对象属性的动态访问(使用attribute函数)。
有没有人知道如何将数据从我的自定义表单类型一直到树枝模板?那就可以了。尽管这当然是多余的,也有点笨拙。
$builder->add('titleFragments',
new Form\DerivedType\SortableCollectionType(), array(
'type' => new TitlefragmentType(),
'options' => array('collectionPropertyName' => 'titleFragments'),我没有发现form builder的Symfony API有什么特别的帮助。
发布于 2013-03-04 20:49:59
要将数据传递给视图,可以使用form类型的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 %}https://stackoverflow.com/questions/13841822
复制相似问题