【发布时间】:2015-12-04 10:09:32
【问题描述】:
我的表格有点问题。我已经制作了表单类,我需要类似于收音机或选择的表单字段。选择的选择我需要从另一种形式的数据库中提取。这是代码,请告诉我问题出在哪里以及如何解决。
我希望在创建 POST 时选择可用页面(它们的名称)并存储它,以便我知道每个帖子它属于哪个页面,并且对于每个页面我查询并显示该页面的帖子..
<?php
namespace AppBundle\AppForm;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Page;
class PostForm extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('content', 'textarea')
->add('visible', 'choice', array(
'choices' => array(
'Yes' => 1,
'No' => 0
)
))
->add('belongToPage', 'choice', array(
'choices' => array (
new Page()
// here i want to pull from class Page names of
//all pages stored and to present
// this names for options in form field
),
'choices_as_values' => true,
'choice_label' => 'getName'
//getName is function from class Page which returns name of page(s)
))
->add('save', 'submit', array('label' => 'Create Post'));
}
public function getName()
{
// TODO: Implement getName() method.
}
}
【问题讨论】:
-
如果您使用学说并假设存在关系,请使用实体表单类型而不是选项。 symfony.com/doc/current/reference/forms/types/entity.html