繁体   English   中英

Symfony2:如何向实体添加标签,属性以表单形式显示(ContactType表单,实体ContactMessage)?

[英]Symfony2: how to add label, attr to Entity to show in form (ContactType form, entity ContactMessage)?

如何将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;

}

我不确定是否理解您的问题,但是可以在FormType内添加标签和占位符:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM