修改symfony生成表单的标签

时间:2017-05-09 16:45:40

标签: php forms symfony

我目前在Symfony有一个工作表单,我有一个公司列表,每个公司名称旁边都有复选框。这样您就可以检查为每个用户分配的公司。该复选框目前显示的是帐户ID,但实体字段的名称也很有帮助#39;同样。你能用两个实体字段构建一个属性吗?这是我的控制器中的表格:

 ->add('companies', 'entity', array(
            'label'         => 'Company',
            'class'         => 'Default\Bundle\Entity\Customer',
            'property' =>   'accountId', //This puts the company id next to the check box
            'multiple'      => true,
            'expanded'      => true,
            'query_builder' => function ($repository)
                {
                    return $repository->createQueryBuilder('c')->orderBy('c.accountId', 'ASC');
                },))
        ->add('Save', 'submit')
        ->getForm();

这就是我想要做的事情:

 ->add('companies', 'entity', array(
       'label'         => 'Company',
       'class'         => 'Default\Bundle\Entity\Customer',
       'property' =>   'accountId' + 'name', // I want to combine my entity fields here
       'multiple'      => true,
       'expanded'      => true,
       'query_builder' => function ($repository)

这里是仅供参考的实体

class Customer
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Assert\NotBlank(message="Please enter a Customer ID.")
     * @Assert\Length(max="32")
     * @ORM\Column(type="string", length=32)
     * @var string
     */
    protected $accountId;

    /**
     * @Assert\NotBlank(message="Please enter a company name.")
     * @Assert\Length(max="60")
     * @ORM\Column(type="string", length=60)
     * @var string
     */
    protected $name;

最后一次......我想离开这个:

Original

对此:

New

2 个答案:

答案 0 :(得分:2)

创建一个简单的getter并将其用作属性,例如:

public function getNamePlusAccountId()
{
    return $this->name." (".$this->accountId.")";
}

并在表单中使用'property' => 'namePlusAccountId'

答案 1 :(得分:1)

如果您只需要更改标签但希望保留表单字段值,那么http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label可能是您要查找的内容