Php 在Symfony get中嵌入表单集合;变量";扩大的;“不存在”;自举
目前,我正在尝试遵循symfony页面中的示例: 但对于我自己的页面。事情没有太大变化,我有一个问题和选择,而不是任务和标签(情况是,我想呈现一个表单,允许我创建一个不同答案的问题,这样我们就可以进行“多项选择考试”) 为了做到这一点,我创建了实体Pregunta(问题)和实体Choice。 由于一个问题可以有多个选择,但这些选择只对应于一个问题,因此关系被映射为多个问题对应于一个问题 这是每个实体的实体代码。(至少包括头部和映射信息) 这里是Choice.phpPhp 在Symfony get中嵌入表单集合;变量";扩大的;“不存在”;自举,php,twitter-bootstrap,symfony,twig,Php,Twitter Bootstrap,Symfony,Twig,目前,我正在尝试遵循symfony页面中的示例: 但对于我自己的页面。事情没有太大变化,我有一个问题和选择,而不是任务和标签(情况是,我想呈现一个表单,允许我创建一个不同答案的问题,这样我们就可以进行“多项选择考试”) 为了做到这一点,我创建了实体Pregunta(问题)和实体Choice。 由于一个问题可以有多个选择,但这些选择只对应于一个问题,因此关系被映射为多个问题对应于一个问题 这是每个实体的实体代码。(至少包括头部和映射信息) 这里是Choice.php namespace Aosh
namespace Aoshido\studyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Choice
*
* @ORM\Table()
* @ORM\Entity
*/
class Choice
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="contenido", type="string", length=1000)
*/
private $contenido;
/**
* @var boolean
*
* @ORM\Column(name="correcto", type="boolean")
*/
private $correcto;
/**
* @var boolean
*
* @ORM\Column(name="activo", type="boolean")
*/
private $activo;
/**
* @ORM\ManyToOne(targetEntity="Pregunta", inversedBy="choices")
* @ORM\JoinColumn(name="idPregunta", referencedColumnName="id")
*/
protected $pregunta;
现在,根据这个例子,当涉及到嵌入表单时,您需要创建一个类型(用于提问),并在该类型内为其他实体创建一个类型(用于选择)。所以我做了
<?php
namespace Aoshido\studyBundle\form;
use Aoshido\studyBundle\form\ChoiceType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PreguntaType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('contenido');
$builder->add('vof');
$builder->add('respuesta');
$builder->add('choices', 'collection', array(
'type' => new ChoiceType(),
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Aoshido\studyBundle\Entity\Pregunta',
));
}
public function getName() {
return 'pregunta';
}
}
在那之后,我只是复制并修改了Javascript代码,以允许它添加多个选项,但在那之后,每次尝试刷新页面时,我都会遇到这个错误
BraincraftedBootstrapBundle:Form:bootstrap.html.twig第173行中不存在变量“expanded”
这是我的树枝
{% extends "AoshidostudyBundle:ABM:abmPreguntas.html.twig" %}
{% block form %}
<div class="panel panel-info">
<div class="panel-heading">Agregar preguntas</div>
<div class="panel-body">
<div class="form-group">
{{ form_start(form) }}
{{ form_row(form.contenido) }}
{{ form_row(form.respuesta) }}
<ul class="choice" data-prototype="{{ form_widget(form.choices.vars.prototype)|e }}">
{% for choice in form.choices %}
<li>
{{ form_row(choice) }}
</li>
{% endfor %}
</ul>
<a href="#" id="add-another-choice">Add another choice</a>
{{ form_end(form) }}
</div>
</div>
</div>
<script type="text/javascript">
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function () {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.choice');
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
</script>
{%endblock%}
我正在使用
- Symfony 2.3.1
- php 5.3.3
- 我想补充一点,但我的声誉还不够。您是否检查了文档中的Symfony版本是否正确?您可以链接到当前的Symfony文档(从现在的2.6版本开始),但您使用的是2.3.1版本
尝试升级到最新版本,然后发现相同的错误
此外,我看到您正在使用BraincraftedBootstrapBundle。这些文档是用于香草Symfony安装的。你查过他们的文件了吗
这看起来像是你想要达到的目标。使用内联表单从页面中动态添加项目的小代码段:
$this->createFormBuilder(array())
->add('repcol', 'bootstrap_collection', array(
'type' => 'repeated',
'allow_add' => true,
'allow_delete' => true,
'sub_widget_col' => 9,
'button_col' => 3,
'prototype_name' => 'inlinep',
'options' => array(
'type' => 'text',
'attr' => array('style' => 'inline')
)
))
->getForm();
与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。请参见“.顺便说一句,这是“提前感谢”,而不是“优势感谢”。您能否准确显示“BraincraftedBootstrapBundle:Form:bootstrap.html.twig”在第173行?
$builder->add('choices', 'collection', array(
'type' => new ChoiceType(),
'allow_add' => true,
));
{% extends "AoshidostudyBundle:ABM:abmPreguntas.html.twig" %}
{% block form %}
<div class="panel panel-info">
<div class="panel-heading">Agregar preguntas</div>
<div class="panel-body">
<div class="form-group">
{{ form_start(form) }}
{{ form_row(form.contenido) }}
{{ form_row(form.respuesta) }}
<ul class="choice" data-prototype="{{ form_widget(form.choices.vars.prototype)|e }}">
{% for choice in form.choices %}
<li>
{{ form_row(choice) }}
</li>
{% endfor %}
</ul>
<a href="#" id="add-another-choice">Add another choice</a>
{{ form_end(form) }}
</div>
</div>
</div>
<script type="text/javascript">
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function () {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.choice');
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
</script>
{%endblock%}
$pregunta = new Pregunta();
$form = $this->createForm(new PreguntaType(), $pregunta, array(
'action' => $this->generateUrl('preguntas_ABM'),
'method' => 'POST',
));
return $this->render('AoshidostudyBundle:ABM:newForm.html.twig', array(
'form' => $form->createView(),
'paginas' => $pagination,
'cantidad' => $cant,
));
$this->createFormBuilder(array())
->add('repcol', 'bootstrap_collection', array(
'type' => 'repeated',
'allow_add' => true,
'allow_delete' => true,
'sub_widget_col' => 9,
'button_col' => 3,
'prototype_name' => 'inlinep',
'options' => array(
'type' => 'text',
'attr' => array('style' => 'inline')
)
))
->getForm();