根据Symfony 3.3中的url参数动态设置表单字段

问题描述 投票:2回答:2

我正在使用Symfony 3.3。我有一个表单需要根据url中的参数动态显示一些字段。

最初向用户显示仅具有主题字段的表单。之后,他选择一个主题,表格中的一些字段要么隐藏,要么显示。有些字段可以为空,因此无需完成。

我创造了一个看起来像这样的Form

就我而言,它感觉和看起来像一团糟。我对symfony很新,关于这个特定场景的文档要么不存在,要么我似乎无法找到它。

const CONTACT_CHOICE_BLANK = 'blank';
const CONTACT_CHOICE_REGISTER = 'register';
const CONTACT_CHOICE_COMPANY = 'company';
const CONTACT_CHOICE_CONTACT = 'contact';

....

/**
 * @return string
 */
private function getChoice() {
    if($this->requestStack->getCurrentRequest()->query->has('subject')) {
        $subject = $this->requestStack->getCurrentRequest()->query->get('subject');
        switch($subject){
            case self::CONTACT_CHOICE_REGISTER:
                $default_choice = 'contact.form.select.option.' . self::CONTACT_CHOICE_REGISTER;
                break;
            case self::CONTACT_CHOICE_COMPANY:
                $default_choice = 'contact.form.select.option.' . self::CONTACT_CHOICE_COMPANY;
                break;
            case self::CONTACT_CHOICE_CONTACT:
                $default_choice = 'contact.form.select.option.' . self::CONTACT_CHOICE_CONTACT;
                break;

            case self::CONTACT_CHOICE_BLANK:
            default:
                $default_choice = 'contact.form.select.option.' . self::CONTACT_CHOICE_BLANK;
                break;
        }
        return $default_choice;
    }

    return self::CONTACT_CHOICE_BLANK;
}

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $default_choice = $this->getChoice();

    $this->session->set('show_message', false);
    $this->session->set('show_email', false);
    $this->session->set('show_company', false);
    $this->session->set('show_email_pro', false);
    $this->session->set('show_company_spouse', false);

    $builder
        ->add('subject', ChoiceType::class, array(
            'choices' => array(
                'contact.form.select.option.' . self::CONTACT_CHOICE_BLANK    => self::CONTACT_CHOICE_BLANK,
                'contact.form.select.option.' . self::CONTACT_CHOICE_REGISTER => self::CONTACT_CHOICE_REGISTER,
                'contact.form.select.option.' . self::CONTACT_CHOICE_COMPANY  => self::CONTACT_CHOICE_COMPANY,
                'contact.form.select.option.' . self::CONTACT_CHOICE_CONTACT  => self::CONTACT_CHOICE_CONTACT,
            ),
            'label' => 'contact.form.select.subject',
            'required' => true,
            'data' => $default_choice,
        ))

        ->add('firstName', TextType::class, array('label' => 'contact.form.input.firstname'))
        ->add('familyName', TextType::class, array('label' => 'contact.form.input.familyname'))
        ->add('phoneNumber', TextType::class, array('label' => 'contact.form.input.phone'))
        ->add('contactReason', ChoiceType::class, array(
                'choices' => array(
                    'contact.form.select.option.advertising' => 'contact.form.select.option.advertising',
                    'contact.form.select.option.internet' => 'contact.form.select.option.internet',
                    'contact.form.select.option.member' => 'contact.form.select.option.member',
                    'contact.form.select.option.word' => 'contact.form.select.option.word',
                    'contact.form.select.option.other' => 'contact.form.select.option.other'),
                'label' => 'contact.form.select.reason'
            ))
        ->add('send', SubmitType::class, array('label' => 'contact.form.textarea.send'));


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

        if($default_choice == 'contact.form.select.option.information_request') {
            $form->add('email', TextType::class, array(
                'label' => 'contact.form.input.email',
            ));
            $this->session->set('show_email', true);
        }

        if($default_choice == 'contact.form.select.option.business_membership_application') {
            $form->add('emailPro', TextType::class, array(
                'label' => 'contact.form.input.emailPro',
            ));
            $form->add('company', TextType::class, array(
                'label' => 'contact.form.input.company',
            ));
            $this->session->set('show_email_pro', true);
            $this->session->set('show_company', true);
        }

        if($default_choice == 'contact.form.select.option.registration_request') {
            $form->add('companySpouse', TextType::class, array(
                'label' => 'contact.form.input.companyspouse',
            ));
            $this->session->set('show_company_spouse', true);
        }

        if($default_choice == 'contact.form.select.option.registration_request' || $default_choice == 'contact.form.select.option.information_request') {
            $form->add('message', TextareaType::class, array(
                'label' => 'contact.form.textarea.message',
            ));
            $this->session->set('show_message', true);
        }
    });
}

在控制器中,处理此表单的函数如下所示:

public function contactAction(Request $request, Mailer $mailer) {

    $contact = new Contact();
    $form = $this->createForm(ContactType::class, $contact, ['allow_extra_fields' => true]);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $mailer->sendContactMail($form);

        $em = $this->getDoctrine()->getManager();
        $em->persist($contact);
        $em->flush();
    }

    return $this->render('WIVCoreBundle:Core:contact.html.twig', array(
        'form' => $form->createView(),
        'routes' => [
            'blank' => $this->generateUrl('wiv_core_contact', ['subject' => ContactType::CONTACT_CHOICE_BLANK]),
            'register' => $this->generateUrl('wiv_core_contact', ['subject' => ContactType::CONTACT_CHOICE_REGISTER]),
            'company' => $this->generateUrl('wiv_core_contact', ['subject' => ContactType::CONTACT_CHOICE_COMPANY]),
            'contact' => $this->generateUrl('wiv_core_contact', ['subject' => ContactType::CONTACT_CHOICE_CONTACT]),
        ],
        'default_route' =>  $this->generateUrl('wiv_core_contact'),
    ));
}

我的问题:是否有更好的方式来显示/隐藏字段?也许某些事情并不像一团糟?

我不需要手握,只是指向正确的方向。也许我错过了文档的某些部分。

php symfony symfony-forms
2个回答
0
投票

是的,有更好的方法来做到这一点。您可以使用表单事件来修改表单的元素。还有一个食谱条目,描述了这个功能:http://symfony.com/doc/current/form/dynamic_form_modification.html

我还强烈推荐Bernhard Schussek(该组件的创建者)的幻灯片:

https://speakerdeck.com/webmozart/symfony2-form-tricks


0
投票

我不确定如果我得到你的问题,我会提供一个答案,如果我弄错了,可以随意发表评论。

为什么不在控制器中的contact实体中设置主题:

public function contactAction(Request $request, Mailer $mailer) {

    $contact = new Contact();
    $contact->setSubject($request->query->get('subject');
    ..... 
}

当将contact实体传递给它已经拥有主题字段的表单时,然后在PRE_SET_DATA事件中,您检查subject实体的contact字段:

 $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($default_choice) {
    $form = $event->getForm();
    $contact = $event->getData();
    if ($contact && $contact->getSubject() == 'value1' ) {
       $form->add(...)
     // do your magic here 
    }));
    }

资料来源:http://symfony.com/doc/2.8/form/dynamic_form_modification.html

© www.soinside.com 2019 - 2024. All rights reserved.