在 Symfony2 表单生成器中允许选择字段类型的所有值
Allow all value on choice field type in Symfony2 form builder
我遇到了一段时间的问题,我已经阅读了很多关于这个主题的类似问题,但无法在我的案例中实施答案。
我有一个 select 字段,我用 Ajax 填充了它。所以在我的表单生成器中我有这个代码:
VilleType.php
/**
* @ORM\Entity(repositoryClass="MDB\AnnonceBundle\Entity\RegisterRepository")
*/
class VilleType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nomComplet', 'choice'
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MDB\AdresseBundle\Entity\Ville'
));
}
/**
* @return string
*/
public function getName() {
return 'mdb_adressebundle_ville';
}
}
但我的表单从未通过验证,因为它们在此选择字段中没有价值。但我无法在其中添加值,因为我事先不知道用户将输入什么作为值。
所以我的问题是如何从 Symfony 禁用对此字段的验证。或者让它接受所有值。
谢谢
EDIT
在这里,我尝试了一种新的方法。我使用事件侦听器修改我的字段,其值比用户提交的值要多。
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nomComplet', 'choice');
$builder->get('nomComplet')->addEventListener(
FormEvents::PRE_SUBMIT, function(FormEvent $event) /* use ($formModifier) */ {
$ville = $event->getData();
$event->getForm()->add('nomComplet', 'choice', array('choices' => $ville));
// $formModifier($event->getForm()->getParent(), $ville);
}
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MDB\AdresseBundle\Entity\Ville'
));
}
/**
* @return string
*/
public function getName() {
return 'mdb_adressebundle_ville';
}
}
MDB\AdresseBundle\Entity\Ville.php
<?php
namespace MDB\AdresseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ville
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MDB\AdresseBundle\Entity\VilleRepository");
*/
class Ville
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nomComplet", type="string", length=255)
*/
private $nomComplet;
/**
* @var string
*
* @ORM\Column(name="nomClean", type="string", length=255)
*/
private $nomClean;
/**
* @var array
*
* @ORM\Column(name="cp", type="simple_array")
*/
private $cp;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nomComplet
*
* @param string $nomComplet
* @return Ville
*/
public function setNomComplet($nomComplet)
{
$this->nomComplet = $nomComplet;
return $this;
}
/**
* Get nomComplet
*
* @return string
*/
public function getNomComplet()
{
return $this->nomComplet;
}
/**
* Set nomClean
*
* @param string $nomClean
* @return Ville
*/
public function setNomClean($nomClean)
{
$this->nomClean = $nomClean;
return $this;
}
/**
* Get nomClean
*
* @return string
*/
public function getNomClean()
{
return $this->nomClean;
}
/**
* Set cp
*
* @param array $cp
* @return Ville
*/
public function setCp($cp)
{
$this->cp = $cp;
return $this;
}
/**
* Get cp
*
* @return array
*/
public function getCp()
{
return $this->cp;
}
public function __toString()
{
return $this->nomComplet;
}
}
但仍然无法正常工作,出现以下错误:
You cannot add children to a simple form. Maybe you should set the option "compound" to true?
因此,如果有人知道如何将这种方式与事件监听器一起使用,那就太好了。
谢谢
这应该有效
https://github.com/LPodolski/choiceAjaxLoad/blob/master/src/AppBundle/Form/ItemType.php
演示此案例的整个项目:
https://github.com/LPodolski/choiceAjaxLoad
案例文件中的代码是 removed/altered:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('choiceField', 'choice', array(
'attr' => array(
'class' => 'choiceField'
)
))
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$form->remove('choiceField');
$form->add('choiceField', 'choice', array(
'attr' => array(
'class' => 'choiceField',
),
'choices' => array(
$data['choiceField'] => $data['choiceField'],
)
));
});
$builder->add('save', 'submit');
}
So my question is how to disable verification on this field from Symfony.
根据 Documentation,您可以使用 POST_SUBMIT 事件抑制表单验证并防止调用 ValidationListener。
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$event->stopPropagation();
}, 900); // Always set a higher priority than ValidationListener
我遇到了一段时间的问题,我已经阅读了很多关于这个主题的类似问题,但无法在我的案例中实施答案。
我有一个 select 字段,我用 Ajax 填充了它。所以在我的表单生成器中我有这个代码:
VilleType.php
/**
* @ORM\Entity(repositoryClass="MDB\AnnonceBundle\Entity\RegisterRepository")
*/
class VilleType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nomComplet', 'choice'
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MDB\AdresseBundle\Entity\Ville'
));
}
/**
* @return string
*/
public function getName() {
return 'mdb_adressebundle_ville';
}
}
但我的表单从未通过验证,因为它们在此选择字段中没有价值。但我无法在其中添加值,因为我事先不知道用户将输入什么作为值。
所以我的问题是如何从 Symfony 禁用对此字段的验证。或者让它接受所有值。
谢谢
EDIT
在这里,我尝试了一种新的方法。我使用事件侦听器修改我的字段,其值比用户提交的值要多。
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('nomComplet', 'choice');
$builder->get('nomComplet')->addEventListener(
FormEvents::PRE_SUBMIT, function(FormEvent $event) /* use ($formModifier) */ {
$ville = $event->getData();
$event->getForm()->add('nomComplet', 'choice', array('choices' => $ville));
// $formModifier($event->getForm()->getParent(), $ville);
}
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MDB\AdresseBundle\Entity\Ville'
));
}
/**
* @return string
*/
public function getName() {
return 'mdb_adressebundle_ville';
}
}
MDB\AdresseBundle\Entity\Ville.php
<?php
namespace MDB\AdresseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ville
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MDB\AdresseBundle\Entity\VilleRepository");
*/
class Ville
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nomComplet", type="string", length=255)
*/
private $nomComplet;
/**
* @var string
*
* @ORM\Column(name="nomClean", type="string", length=255)
*/
private $nomClean;
/**
* @var array
*
* @ORM\Column(name="cp", type="simple_array")
*/
private $cp;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nomComplet
*
* @param string $nomComplet
* @return Ville
*/
public function setNomComplet($nomComplet)
{
$this->nomComplet = $nomComplet;
return $this;
}
/**
* Get nomComplet
*
* @return string
*/
public function getNomComplet()
{
return $this->nomComplet;
}
/**
* Set nomClean
*
* @param string $nomClean
* @return Ville
*/
public function setNomClean($nomClean)
{
$this->nomClean = $nomClean;
return $this;
}
/**
* Get nomClean
*
* @return string
*/
public function getNomClean()
{
return $this->nomClean;
}
/**
* Set cp
*
* @param array $cp
* @return Ville
*/
public function setCp($cp)
{
$this->cp = $cp;
return $this;
}
/**
* Get cp
*
* @return array
*/
public function getCp()
{
return $this->cp;
}
public function __toString()
{
return $this->nomComplet;
}
}
但仍然无法正常工作,出现以下错误:
You cannot add children to a simple form. Maybe you should set the option "compound" to true?
因此,如果有人知道如何将这种方式与事件监听器一起使用,那就太好了。
谢谢
这应该有效
https://github.com/LPodolski/choiceAjaxLoad/blob/master/src/AppBundle/Form/ItemType.php
演示此案例的整个项目: https://github.com/LPodolski/choiceAjaxLoad
案例文件中的代码是 removed/altered:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('choiceField', 'choice', array(
'attr' => array(
'class' => 'choiceField'
)
))
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$form->remove('choiceField');
$form->add('choiceField', 'choice', array(
'attr' => array(
'class' => 'choiceField',
),
'choices' => array(
$data['choiceField'] => $data['choiceField'],
)
));
});
$builder->add('save', 'submit');
}
So my question is how to disable verification on this field from Symfony.
根据 Documentation,您可以使用 POST_SUBMIT 事件抑制表单验证并防止调用 ValidationListener。
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$event->stopPropagation();
}, 900); // Always set a higher priority than ValidationListener