Symfony2:如何添加标签,attr到Entity以表单形式显示(ContactType表单,实体ContactMessage)?

时间:2017-04-18 08:29:32

标签: php forms symfony entity

如何将label, trim, attr => [placeholder => "Field name"]添加到实体以在表单中显示(表单ContactMessageType,实体ContactMessage)? 我试图在一个文件中配置表单字段和实体字段 - >实体类ContactMessage。 那可能吗?如果有,怎么样? 如果不是,该怎么办?

我的实体:

 use Symfony\Component\Validator\Constraints as Assert;
 use Doctrine\ORM\Mapping as ORM;
 /**
 * php app/console doctrine:schema:validate
 * php app/console doctrine:schema:update --force
 *
 * Class ContactMessage
 * @see http://symfony.com/doc/2.8/doctrine/registration_form.html
 *
 * @ORM\Entity
 * @ORM\Table(name="contact_messages", options={"collate"="utf8_general_ci"})
 */
class ContactMessage
{
    public function __construct()
    {
        $this->createdDateTime = new \DateTime("now");
    }

/**
 * @ORM\Column(type="integer", nullable=false, options={"unsigned":true, "length":11})
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 *
 * @var integer
 */
protected $id;
/**
 * @Assert\NotBlank()
 *
 * @ORM\Column(type="string", name="sender_name", nullable=false, length=255, options={})
 *
 * @var string
 */
protected $senderName;
/**
 * @Assert\NotBlank()
 *
 * @ORM\Column(type="string", name="sender_email", nullable=false, length=512, options={"default":""})
 *
 * @var string
 */
protected $senderEmail;
/**
 *
 *
 * @ORM\Column(type="string", name="sender_message", nullable=false, length=4096, options={"default":""})
 *
 * @var string
 */
protected $senderMessage;
/**
 *
 * @ORM\Column(type="string", name="sender_phone_number", nullable=false, length=16, options={"default":""})
 *
 * @var string
 */
protected $senderPhoneNumber;

/**
 * @ORM\Column(type="datetime", name="created_date_time", nullable=false,  options={"default": "0000-00-00 00:00:00"})
 *
 * @var \DateTime
 */
protected $createdDateTime;

}

1 个答案:

答案 0 :(得分:0)

我不确定您的问题,但您可以在FormType中添加标签和占位符:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('senderName', TextType::class, [
            'required' => false,
            'label'    => 'Your label',
            'attr'     => [
                'placeholder' => 'Your placeholder',
            ],
        ]);
}