Javascript 表单嵌入Symfony2数据原型为空
我正在尝试在symfony2的子表单中嵌入多个文件上载。 我有一个“想法”的形式,我想有多个“文件”的形式呈现。 我已经遵循了文档,但是当数据原型在表单中显示为Null时,我陷入了停顿。据我所知,原型是一个呈现formtype的html字符串。我已经搜索过了,似乎我是唯一一个得到这个问题的人。 这一定是件小事,但任何帮助都将不胜感激。 我的表格类型:Javascript 表单嵌入Symfony2数据原型为空,javascript,forms,symfony,twig,Javascript,Forms,Symfony,Twig,我正在尝试在symfony2的子表单中嵌入多个文件上载。 我有一个“想法”的形式,我想有多个“文件”的形式呈现。 我已经遵循了文档,但是当数据原型在表单中显示为Null时,我陷入了停顿。据我所知,原型是一个呈现formtype的html字符串。我已经搜索过了,似乎我是唯一一个得到这个问题的人。 这一定是件小事,但任何帮助都将不胜感激。 我的表格类型: public function buildForm(FormBuilderInterface $builder, array $optio
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('category', 'entity',
array('class' => 'AcmeIdeaBundle:Category',
'property' => 'name',
))
->add('description', 'textarea')
->add('file','file')
->add('video')
->add('documents', 'collection', array('type' => new DocumentType,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true));
}
文档formtype生成器:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file');
}
相关细枝文件:
{% block body %}
<form action="{{ path('idea_create') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
{% for document in form.documents %}
<li> {{ form_row(document.file) }} </li>
{% endfor %}
</ul>
{%block body%}
{{form_widget(form)}
{form.documents%中的文档为%s}
- {{form_row(document.file)}
{%endfor%}
如果我在控制器中硬编码一些文档对象,它们看起来很好并且可以编辑,但是我需要它是动态的
javascript代码:
var collectionHolder = $('ul.docs');
var $addDocumentLink = $('<a href="#" class="add_documents_link">Add a picture</a>');
var $newLinkLi = $('<li></li>').append($addDocumentLink);
jQuery(document).ready(function(){
collectionHolder.find('li').each(function(){
addDocumentFormDeleteLink($(this));
});
collectionHolder.append($newLinkLi);
$addDocumentLink.on('click', function(e) {
e.preventDefault();
addDocumentForm(collectionHolder, $newLinkLi);
});
});
function addDocumentForm(collectionHolder, $newLinkLi) {
var prototype = collectionHolder.attr('data-prototype');
var newForm = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length);
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
addDocumentFormDeleteLink($newFormLi);
}
function addDocumentFormDeleteLink($docFormLi) {
var $removeFormA = $('<a href= "#">remove?</a>');
$docFormLi.append($removeFormA);
$removeFormA.on('click', function(e) {
e.preventDefault();
$docFormLi.remove();
});
}
var collectionHolder=$('ul.docs');
var$addDocumentLink=$('');
变量$newLinkLi=$(').append($addDocumentLink);
jQuery(文档).ready(函数(){
collectionHolder.find('li')。每个(函数(){
addDocumentFormDeleteLink($(此));
});
collectionHolder.append($newLinkLi);
$addDocumentLink.on('click',函数(e){
e、 预防默认值();
addDocumentForm(收款人,$newLinkLi);
});
});
函数addDocumentForm(collectionHolder$newLinkLi){
var-prototype=collectionHolder.attr('data-prototype');
var newForm=prototype.replace(/\$\$name\$\$$/g,collectionHolder.children().length);
变量$newFormLi=$('')。追加(newForm);
$newLinkLi.before($newFormLi);
addDocumentFormDeleteLink($newFormLi);
}
函数addDocumentFormDeleteLink($docFormLi){
var$removeFormA=$('');
$docFormLi.append($removeFormA);
$removeFormA.on('click',函数(e){
e、 预防默认值();
$docFormLi.remove();
});
}
这也是我第一次在这里问问题,所以如果我做错了什么,我道歉 由于某种原因,当我将代码放入细枝文件时
<ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
{% for document in form.documents %}
<li> {{ form_row(document.file) }} </li>
{% endfor %}
{form.documents%中的文档为%s}
- {{form_row(document.file)}
{%endfor%}
变成一个有标签的divie
<div class="tab-pane" id="details">
{{ form_row(form.video) }}
<ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
{% for document in form.documents %}
<li> {{ form_row(document.path) }} </li>
{% endfor %}
</ul>
{{ form_row(form.description) }}
</div>
{{form_row(form.video)}
{form.documents%中的文档为%s}
- {{form_row(document.path)}
{%endfor%}
{{form_row(form.description)}}
现在原型不是空的,并且由于某种原因,当我将代码放入twig文件时,一切都很好
<ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
{% for document in form.documents %}
<li> {{ form_row(document.file) }} </li>
{% endfor %}
{form.documents%中的文档为%s}
- {{form_row(document.file)}
{%endfor%}
变成一个有标签的divie
<div class="tab-pane" id="details">
{{ form_row(form.video) }}
<ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
{% for document in form.documents %}
<li> {{ form_row(document.path) }} </li>
{% endfor %}
</ul>
{{ form_row(form.description) }}
</div>
{{form_row(form.video)}
{form.documents%中的文档为%s}
- {{form_row(document.path)}
{%endfor%}
{{form_row(form.description)}}
现在原型不是空的,一切都很好您有哪个版本的Symfony?表单原型从2.0版更改为2.1Hi版,感谢您的回复。我正在运行symfony 2.1.1您有哪一版本的symfony?表单原型从2.0版更改为2.1Hi版,感谢您的回复。我正在运行symfony 2.1.1