使用Symfony 3

时间:2017-02-03 16:15:47

标签: symfony

我有分支机构注册表格,在我的表格中我有两个下拉字段company_id& sub_agent_id

我希望根据用户在sub_agent_id字段中选择的内容自动填充company_id字段。

假设用户在我的company_a字段中选择idcompany的{​​{1}},那么在我的sub_agent_id字段中必须显示所有数据(自动)我的1实体中的值为sub_agent,依此类推。

我在symfony中发现了一篇关于此问题的好文章,Dynamic Generation for Submitted Forms但我必须说我还是一个菜鸟,我无法找到它。

这是我的company实体数据

+++++++++++++++++++
| id  |  name     |
| 1   | company_a |
| 2   | company_b |
+++++++++++++++++++

这是我的sub_agent实体

++++++++++++++++++++++++++++++++
| id   |  company_id | name    |
| 13   |    1        | Google  |
| 14   |    1        | Apple   |
| 15   |    2        | Oracle  |
++++++++++++++++++++++++++++++++

这是我的代码

在我的branchType.php

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;


class BranchType extends AbstractType {


    public function buildForm(FormBuilderInterface $builder, array $options)
    {

    $builder->add('company_id', EntityType::class, array(
                  'class'=>'AppBundle\Entity\Company', 
                  'query_builder' => function (\AppBundle\Repository\CompanyRepository $er) { 
                    return $er->createQueryBuilder('s')
                         ->where('s.status = 0')
                         ->orderBy('s.id', 'ASC');
                    },
                  'choice_label' => 'name', 
                  'placeholder' => 'Select Company',
                ));

    $builder->addEventListener( FormEvents::PRE_SET_DATA, function (FormEvent $event) {
                    $form = $event->getForm();

                    // this would be your entity, i.e. BranchType
                    $data = $event->getData();

                    $company = $data->getCompanyId();

                    $companies = null === $company ? array() : $company->getCompanyId();

                    $form->add('sub_agent_id', EntityType::class, array(
                        'class'       => 'AppBundle\Entity\Sub_agent',
                        'placeholder' => '',
                        'choices'     => $companies,
                    ));
                }
            );

    $formModifier = function (FormInterface $form, Company $company = null) {
                $fields = null === $company ? array() : $company->get();

                $form->add('sub_agent_id', EntityType::class, array(
                    'class'       => 'AppBundle\Entity\Sub_agent',
                    'placeholder' => '',
                    'choices' => $fields,
                ));
            };

    $builder->addEventListener(
                FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) {
                    $data = $event->getData();
                    $formModifier($event->getForm(), $data->getName());
                }
            );            


    $builder->get('company_id')->addEventListener(
                FormEvents::POST_SUBMIT, function (FormEvent $event) use ($formModifier) {
                    $outTable = $event->getForm()->getData();
                    $formModifier($event->getForm()->getParent(), $company);
                }
            );

     $builder->addEventListener(
                FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) {
                    // this would be your entity, i.e. SportMeetup
                    $data = $event->getData();

                    $formModifier($event->getForm(), $data->getCompanyId());
                }
            );

欣赏回应:)

0 个答案:

没有答案