Forms Symfony2将自定义字段添加到表单生成器
我有一个实体事件,它有一个字段关键字,它将一对多映射到另一个实体事件关键字。我还有一个表单类型类CreateEventFormType。我使用以下代码在控制器中创建表单:Forms Symfony2将自定义字段添加到表单生成器,forms,symfony,Forms,Symfony,我有一个实体事件,它有一个字段关键字,它将一对多映射到另一个实体事件关键字。我还有一个表单类型类CreateEventFormType。我使用以下代码在控制器中创建表单: $event = new Event(); $form = $this->createForm(new CreateEventFormType(), $event); 但我还需要额外的关键字输入字段,用于创建对象关键字。我已尝试将其添加到我的formBuilderInterface中: ->add
$event = new Event();
$form = $this->createForm(new CreateEventFormType(), $event);
但我还需要额外的关键字输入字段,用于创建对象关键字。我已尝试将其添加到我的formBuilderInterface中:
->add('keywords', 'text', [
'constraints' =>[
new Assert\NotBlank([
'message' => "Renginio raktažodžiai negali būti tušti"
]),
new Assert\Length([
'min' => "2",
'max' => "255",
'minMessage' => "Renginio raktažodžiai negali būti trumpesni nei {{ limit }} simboliai",
'maxMessage' => "Renginio raktažodžiai negali būti ilgesni nei {{ limit }} simboliai"
])
]
])
但是我得到了一个错误
属性“keywords”或方法“AddKeywords()”/“RemoveKeywords()”、“setKeywords()”、“keywords()”、“\uuu set()”或“\uu call()”都不存在,并且在类“Atotrukis\MainBundle\Entity\Event”中都具有公共访问权限。
完整实体和表单类型代码:
createEventFormType.php
<?php
namespace Atotrukis\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Validator\Constraints as Assert;
class CreateEventFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', [
'constraints' =>[
new Assert\NotBlank([
'message' => "Renginio pavadinimas negali būti tuščias"
]),
new Assert\Length([
'min' => "2",
'max' => "255",
'minMessage' => "Renginio pavadinimas negali būti trumpesnis nei {{ limit }} simboliai",
'maxMessage' => "Renginio pavadinimas negali būti ilgesnis nei {{ limit }} simboliai"
])
]
])
->add('description', 'textarea', [
'constraints' =>[
new Assert\NotBlank([
'message' => "Renginio aprašymas negali būti tuščias"
]),
new Assert\Length([
'min' => "10",
'max' => "5000",
'minMessage' => "Renginio aprašymas negali būti trumpesnis nei {{ limit }} simbolių",
'maxMessage' => "Renginio aprašymas negali būti ilgesnis nei {{ limit }} simbolių"
])
]
])
->add('startDate', 'datetime', [
'constraints' =>[
new \Atotrukis\MainBundle\Validator\Constraints\FutureDateTime([
'message' => "Pradžios laikas negali būti ankstesnis už dabartinį laiką."
])
]
])
->add('endDate', 'datetime', [
'constraints' =>[
new \Atotrukis\MainBundle\Validator\Constraints\FutureDateTime([
'message' => "Pabaigos laikas negali būti ankstesnis už dabartinį laiką."
])
]
])
->add('keywords', 'text', [
'constraints' =>[
new Assert\NotBlank([
'message' => "Renginio raktažodžiai negali būti tušti"
]),
new Assert\Length([
'min' => "2",
'max' => "255",
'minMessage' => "Renginio raktažodžiai negali būti trumpesni nei {{ limit }} simboliai",
'maxMessage' => "Renginio raktažodžiai negali būti ilgesni nei {{ limit }} simboliai"
])
]
])
->add('map', 'hidden')
->add('city', 'entity', array(
'class' => 'AtotrukisMainBundle:City',
'property' => 'name',
'constraints' =>[
new Assert\NotBlank([
'message' => "Privalote pasirinkti miestą"
])
],
'empty_value' => 'Pasirinkite miestą',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')
->addOrderBy('c.priority', 'ASC')
->addOrderBy('c.name', 'ASC');
},
));
// ->add('save', 'submit', array('label' => 'Sukurti'));
}
public function getName()
{
return 'createEventForm';
}
}
您必须做的是:
->add('yourfield', 'choice', array(
'label' => 'Your Field',
'required' => false,
'choices' => array(true => 'Yes', false => 'No'),
'empty_value' => false,
'mapped' => false
))
注意“映射”=>false。这意味着该字段与实体(对象)无关。在你们班上根本就没有
这样,您就可以添加任意多的附加字段。事件实体中的关键字设置器在哪里?我找不到它。另外,您应该初始化关键字
集合(在\uu构造中
使用数组集合
),我已将关键字设置器添加到事件实体中。还尝试添加$this->keywords=newarraycollection()
到_构造,但现在我得到错误:可捕获的致命错误:传递到Atotrukis\MainBundle\Entity\Event::setKeywords()的参数1必须是Atotrukis\MainBundle\Entity\EventKeywords的实例,给定字符串,在第438行的/var/www/vendor/symfony/symfony/src/symfony/Component/PropertyAccess/PropertyAccessor.php中调用,并在/var/www/src/Atotrukis/MainBundle/Entity/Event.php第389行中定义。我看到您添加了setKeyWords
,但没有看到collection init。2.因为您已经设置了一个对象关联,所以该字段应该包含对象的集合,而不是单个的EventKeywords
object我该怎么做?我对Symfony和OOP都很陌生。
<?php
namespace Atotrukis\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="event_keywords")
*/
class EventKeywords
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $keyword;
/**
* @ORM\ManyToOne(targetEntity="Event", inversedBy="keywords")
* @ORM\JoinColumn(name="eventId", referencedColumnName="id")
*/
protected $eventId;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set eventId
*
* @param \Atotrukis\MainBundle\Entity\Event $eventId
* @return UserAttending
*/
public function setEventId(\Atotrukis\MainBundle\Entity\Event $eventId = null)
{
$this->eventId = $eventId;
return $this;
}
/**
* Get eventId
*
* @return \Atotrukis\MainBundle\Entity\Event
*/
public function getEventId()
{
return $this->eventId;
}
/**
* Set keyword
*
* @param string $keyword
* @return EventKeywords
*/
public function setKeyword($keyword)
{
$this->keyword = $keyword;
return $this;
}
/**
* Get keyword
*
* @return string
*/
public function getKeyword()
{
return $this->keyword;
}
}
->add('yourfield', 'choice', array(
'label' => 'Your Field',
'required' => false,
'choices' => array(true => 'Yes', false => 'No'),
'empty_value' => false,
'mapped' => false
))