【发布时间】:2015-09-13 15:00:17
【问题描述】:
我是 Symfony 的新手。我想做表格的收集。 我得到了类似的东西:
class SkillType extends AbstractType
public function getName()
{
return 'skills_cfg';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' = 'name'
))
->add('name_short', 'text', array(
'label' => 'short name'
));
}
}
我得到了一个我想在该表单上使用集合的表单:
class AbsType extends AbstractType
{
private $object;
public function __construct($object)
{
$this->object = $object;
}
public function getName()
{
return '';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('row', 'collection', array(
'type' => $this->object,
'allow_add' => true,
'prototype' => true,
))
->add('addMore', 'button', array(
'attr' => array(
'value' => 'add more'
),
))
->add('submit', 'submit', array(
'attr' => array(
'value'=>'save',
),
'label' => 'save'
));
}
}
在我的控制器中,我得到了这样的表单:
class InstallController extends Controller
{
public function indexAction($configPage, Request $request)
{
$skillForm= $this->createForm(new AbsType(new SkillType()));
$SkillConfig=new SkillsCfg();
if($request->isMethod('POST')) {
$skillForm->handleRequest($request);
if ($skillForm->isValid()) {
$em=$this->getDoctrine()->getManager();
$em->persist($SkillConfig);
$em->flush();
}
}
return array(
'form' => $skillForm->createView(),
'page' => $configPage
);
}
实体和视图如下所示:
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="name_short", type="string", length=10)
*/
private $nameShort;
当然,我是 getter 和 setter。
查看:
var emailCount = '{{ form.row|length }}';
jQuery(document).ready(function() {
jQuery('#addMore').click(function(e) {
e.preventDefault();
var emailList = jQuery('#fields-list');
// grab the prototype template
var newWidget = emailList.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, emailCount);
emailCount++;
// create a new list element and add it to the list
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(emailList);
});
})
</script>
{% endblock javascripts %}
{% block body %}
{{ form_start(form) }}
<ul id="fields-list"
data-prototype="{{ form_widget(form.row.vars.prototype)|e }}">
{% for newRow in form.row %}
<li>
{{ form_errors(newRow) }}
{{ form_widget(newRow) }}
</li>
{% endfor %}
</ul>
{{ form_end(form) }}
{% endblock body %}
我得到一个错误:
执行 'INSERT INTO Skills_cfg (name, name_short) VALUES (?, ?)' with params [null, null]:
SQLSTATE[23000]:违反完整性约束:1048 列“名称” 不能为空
为什么会这样?我该如何解决?
【问题讨论】:
-
如果我在控制器中更改该行: $skillForm= $this->createForm(new AbsType(new SkillType())); $SkillConfig=新 SkillsCfg();到 $SkillConfig=new SkillsCfg(); $skillForm= $this->createForm(new AbsType(new SkillType()),$SkillConfig);我收到错误属性“row”和方法之一“getRow()”、“row()”、“isRow()”、“hasRow()”、“__get()”都不存在并且在类中具有公共访问权限“引擎\SettingBundle\Entity\SkillsCfg”。但是我必须使用什么名称的 Collection 标签?
标签: forms symfony collections entity