Symfony 2.7我需要两个选择器

时间:2015-09-07 14:25:54

标签: symfony

Symfony 2我需要两个选择器,我有这个代码

use Ct\CiudadBundle\Entity\Departamento;

use Ct\CiudadBundle\Entity\Factura;

use Ct\CiudadBundle\Entity\Provincia;

class FacturaType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //->add('nombre')
            ->add('nit')
            ->add('fidProvincia')
            /*->add('fidDepartamento', 'entity', array(
                'class'       => 'CtCiudadBundle:Departamento',
                'placeholder' => 'Elije el departamento',
            )); */
        ;


        $factory = $builder->getFormFactory();

        $refreshStates = function ($form, $departamento) use ($factory) {
            $form->add($factory->createNamed('entity','provincia', null, array(
                'class'         => 'CtCiudadBundle:Provincia',
                'property'      => 'nombre',
                'empty_value'   => '-- Select a provincia --',
                'query_builder' => function (EntityRepository $repository) use ($departamento) {
                    $qb = $repository->createQueryBuilder('provincia')
                        ->innerJoin('provincia.departamento', 'departamento');

                    if ($departamento instanceof Departamento) {
                        $qb->where('provincia.departamento = :departamento')
                            ->setParameter('departamento', $departamento);
                    } elseif (is_numeric($departamento)) {
                        $qb->where('departamento.id = :departamento')
                            ->setParameter('departamento', $departamento);
                    } else {
                        $qb->where('departamento.nombre = :departamento')
                            ->setParameter('departamento', null);
                    }

                    return $qb;
               })
           ));
        };

        $setDepartamento = function ($form, $departamento) use ($factory) {
            $form->add($factory->createNamed('entity', 'departamento', null, array(
                'class'         => 'CtCiudadBundle:Departamento', 
                'property'      => 'nombre', 
                'property_path' => false,
                'empty_value'   => '-- Select a country --',
                'data'          => $country,
            )));
        };

         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($refreshStates, $setDepartamento) {
            $form = $event->getForm();
            $data = $event->getData();

            if ($data == null) {
                return;
            }

            if ($data instanceof Factura) {
                $departamento = ($data->getId()) ? $data->getProvincia()->getDepartamento() : null ;
                $refreshStates($form, $departamento);
                $setDepartamento($form, $departamento);
            }
        });

        $builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) use ($refreshStates) {
            $form = $event->getForm();
            $data = $event->getData();

            if(array_key_exists('departamento', $data)) {
                $refreshStates($form, $data['departamento']);
            }
        });

我有这个问题

  

捕获致命错误:参数1传递给   克拉\ CiudadBundle \表格\ FacturaType ::克拉\ CiudadBundle \表格{闭合}()   必须是Symfony \ Component \ Form \ Event \ DataEvent的一个实例,   给出

的Symfony \ Component \ Form \ FormEvent实例

我该如何处理? 请帮帮我...... !!

0 个答案:

没有答案