I have a problem for a while and i have read a lot of topic with similar problem but cant implement the answer in my case.
I have a select field that i populate with Ajax. so in my form builder i have this code :
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';
}
}
But my form is never valid because their is no value in this choice field. But i cant add the value inside cause i cant know in advance what the user will put as value.
So my question is how to disable verification on this field from symfony. Or let it allow all value.
Thanks
EDIT
Here, i tried a new approch. I use The event listner to modify my field with the value than the user submitted.
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;
}
}
But it still not working, i have the following error :
You cannot add children to a simple form. Maybe you should set the option "compound" to true?
So if someone know how to use this way with eventListener it would be great.
Thx
Aucun commentaire:
Enregistrer un commentaire