我有一个表单集合,其中我想用一个事件链接两个下拉框。
当我调用$data->getCursValutar()
时,我无法使事件监听器工作,因为我得到一个空值。
这是代码:
<?php
namespace Mnv\CoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ClientiType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$idSocietate = $options['data']->getSocietati()->getIdSocietate();
// var_dump($dateClienti);
$builder->add('cod','text', array('required' => false));
$builder->add('denumire','text', array('required' => true));
$builder->add('cod_fiscal','text', array('required' => true));
$builder->add('cont_analitic','text', array('required' => false));
$builder->add('telefon','text', array('required' => false));
$builder->add('email','email', array('required' => false));
$builder->add('judet','text', array('required' => false));
$builder->add('adresa','text', array('required' => false));
$builder->add('nr_reg_comertului','text', array('required' => false));
$builder->add('cont','text', array('required' => false));
$builder->add('banca','text', array('required' => false));
$builder->add('delegat','text',array('required' => false));
$builder->add('delegat_serie_ci','text',array('required' => false));
$builder->add('delegat_nr_ci','text',array('required' => false));
$builder->add('delegat_ci_eliberat','text',array('required' => false));
$builder->add('delegat_mij_transport','text',array('required' => false));
$builder->add('agent','text',array('required' => false));
$builder->add('discount','text',array('required' => false));
$builder->add('date_clienti', 'collection', array(
'type' => new DateClientiType($idSocietate),
'cascade_validation' => true,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
));
$builder->add('adauga','submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mnv\CoreBundle\Entity\Clienti',
'cascade_validation' => true,
));
}
public function getName()
{
return 'form_clienti';
}
}
第二种形式:
<?php
namespace Mnv\CoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Mnv\CoreBundle\Entity\Orase;
class DateClientiType extends AbstractType
{
private $idSocietate;
function __construct($idSocietate) {
$this->idSocietate = $idSocietate;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
$event->stopPropagation();
}, 900);
$builder->add('suprafata_inchiriata','text',array('required' => false));
$builder->add('suma_chirie','text',array('required' => false));
$builder->add('suma_fixa_chirie','text',array('required' => false));
$builder->add('curs_bnr','checkbox', array('required' => false));
$builder->add('platitor_tva','checkbox', array('required' => false));
$builder->add('fact_auto','checkbox', array('required' => false));
$builder->add('nr_contract','text',array('required' => false));
$builder->add('data_contract','date', array('required' => false, 'widget' => 'single_text', 'format'=>'dd/MM/yyyy'));
$builder->add('act_aditional','text',array('required' => false));
$builder->add('data_actaditional','date', array('required' => false, 'widget' => 'single_text', 'format'=>'dd/MM/yyyy'));
$builder->add('strada_spatiu','text',array('required' => false));
$builder->add('etaj_spatiu','text',array('required' => false));
$builder->add('pozitie_spatiu','text',array('required' => false));
$builder->add('achitare','text',array('required' => false));
$builder->add('tip_fact_utilitati', 'choice', array(
'choices' => array(
'cumulata' => 'Cumulata',
'unitara' => 'Unitara',
'separata' => 'Separata'
),
'required' => false,
'expanded' => false,
'multiple' => false,
'invalid_message' => 'Valoarea aleasa nu este valida!',
'empty_value' => false
));
$builder->add('orase', 'entity', array(
'class' => 'MnvCoreBundle:Orase',
'property' => 'oras',
'empty_value' => 'Selectati',
));
$idSocietate = $this->idSocietate;
$formModifier = function (FormInterface $form, Orase $orase = null) use ($idSocietate) {
$positions = array();
if (null !== $orase) {
$positions = $orase;
}
$form->add('cursuri', 'entity', array(
'class' => 'MnvCoreBundle:CursValutar',
'query_builder' => function(EntityRepository $er) use ($idSocietate,$positions) {
return $er->createQueryBuilder('c')
->where('c.societati = :idSocietate AND c.orase = :oras')
->setParameter('idSocietate', $idSocietate)
->setParameter('oras', $positions)
->orderBy('c.idCurs', 'ASC');
},
'empty_value' => 'Selectati',
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. SportMeetup
$data = $event->getData();
var_dump($data); exit;
$formModifier($event->getForm(), $data->getCursValutar());
}
);
$builder->get('orase')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$oras = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier($event->getForm()->getParent(), $oras);
}
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mnv\CoreBundle\Entity\DateClienti',
'cascade_validation' => true,
));
}
public function getName()
{
return 'date_clienti';
}
}
我是symfony的新手,我正在努力学习它。我做错了什么?
答案 0 :(得分:1)
您的问题已在此处得到解答:Symfony2 PRE_SET_DATA $event->getData() return wrong object 这是在相关的问题块中。
答案 1 :(得分:0)
确定!经过3天的痛苦,我找到了解决方案:): 当然,这很简单。
阅读评论:
<?php
namespace Mnv\CoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Mnv\CoreBundle\Entity\Orase;
use Mnv\CoreBundle\Entity\DateClienti;
class DateClientiType extends AbstractType
{
private $idSocietate;
function __construct($idSocietate) {
$this->idSocietate = $idSocietate;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
$event->stopPropagation();
}, 900);
$builder->add('suprafata_inchiriata','text',array('required' => false));
$builder->add('suma_chirie','text',array('required' => false));
$builder->add('suma_fixa_chirie','text',array('required' => false));
$builder->add('curs_bnr','checkbox', array('required' => false));
$builder->add('platitor_tva','checkbox', array('required' => false));
$builder->add('fact_auto','checkbox', array('required' => false));
$builder->add('nr_contract','text',array('required' => false));
$builder->add('data_contract','date', array('required' => false, 'widget' => 'single_text', 'format'=>'dd/MM/yyyy'));
$builder->add('act_aditional','text',array('required' => false));
$builder->add('data_actaditional','date', array('required' => false, 'widget' => 'single_text', 'format'=>'dd/MM/yyyy'));
$builder->add('strada_spatiu','text',array('required' => false));
$builder->add('etaj_spatiu','text',array('required' => false));
$builder->add('pozitie_spatiu','text',array('required' => false));
$builder->add('achitare','text',array('required' => false));
$builder->add('tip_fact_utilitati', 'choice', array(
'choices' => array(
'cumulata' => 'Cumulata',
'unitara' => 'Unitara',
'separata' => 'Separata'
),
'required' => false,
'expanded' => false,
'multiple' => false,
'invalid_message' => 'Valoarea aleasa nu este valida!',
'empty_value' => false
));
$builder->add('orase', 'entity', array(
'class' => 'MnvCoreBundle:Orase',
'property' => 'oras',
'empty_value' => 'Selectati',
));
$idSocietate = $this->idSocietate;
$formModifier = function (FormInterface $form, Orase $orase = null) use ($idSocietate) {
$positions = array();
if (null !== $orase) {
$positions = $orase;
}
$form->add('curs_valutar', 'entity', array(
'class' => 'MnvCoreBundle:CursValutar',
'query_builder' => function(EntityRepository $er) use ($idSocietate,$positions) {
return $er->createQueryBuilder('c')
->where('c.societati = :idSocietate AND c.orase = :oras')
->setParameter('idSocietate', $idSocietate)
->setParameter('oras', $positions)
->orderBy('c.idCurs', 'ASC');
},
'empty_value' => 'Selectati',
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. SportMeetup
$data = $event->getData();
//var_dump($data); exit;
// so after var dumping i noticed the $data was null
// i tried many solutions found online with no success
// so the problem was the error i got when getOrase was called on a non
// object, so i need it an object and here it is:
if ($data !== null) { // chack to see if $data is not null
// now we can safely call getOrase() on event
$formModifier($event->getForm(), $data->getOrase());
} else { // if it is null
$data = new DateClienti; // instantiate the class
// (don't forget to import the class)
$formModifier($event->getForm(), $data->getOrase()); // no error now :)
}
}
);
$builder->get('orase')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$oras = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier($event->getForm()->getParent(), $oras);
}
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mnv\CoreBundle\Entity\DateClienti',
'cascade_validation' => true,
));
}
public function getName()
{
return 'date_clienti';
}
}