如何在Symfony中创建包含两个表单的页面?

时间:2018-01-11 12:41:55

标签: php forms symfony symfony-forms symfony3.x

我想有一个包含两种表格的页面

example of what I want

这两个表单更新同一个实体:帐户。

我做了什么:

  • 创建了两种表单(AccountGeneralDataType.php& AccountPasswordType.php
  • 将其添加到AccountController.php方法
  • 下的editAction

AccountGeneralDataType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class AccountGeneralDataType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /**
         * todo: Missing avatar upload
         */
        $builder
            ->add('username', TextType::class, [
                'label' => 'Username'
            ])
            ->add('email', EmailType::class, [
                'label' => 'Email'
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Save changes',
                'attr' => [
                    'class' => 'btn btn-outline-primary float-right'
                ]
            ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Account'
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_account';
    }

}

AccountPasswordType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Validator\Constraints\NotBlank;

class AccountPasswordType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('password', PasswordType::class, [
                'label' => 'Old password',
            ])
            ->add('new-password', RepeatedType::class, [
                'label' => 'New password',
                'type' => PasswordType::class,
                'invalid_message' => 'The new password fields must match.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => true,
                'first_options' => [
                    'label' => 'New password'
                ],
                'second_options' => [
                    'label' => 'Repeat password'
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Update password',
                'attr' => [
                    'class' => 'btn btn-outline-primary float-right'
                ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
//            'data_class' => 'AppBundle\Entity\Account'
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_account';
    }

}

AccountController.php

public function editAction(Request $request, Account $account)
{
    $originalAccount = new Account();
    $originalAccount->setEmail($account->getEmail());
    $originalAccount->setUsername($account->getUsername());
    $originalAccount->setAvatar($account->getAvatar());

    /**
     * Form to edit general information
     */
    $editAccountForm = $this->createForm('AppBundle\Form\AccountGeneralDataType', $account);
    $editAccountForm->handleRequest($request);

    /**
     * Form to edit password
     */
    $editPasswordForm = $this->createForm('AppBundle\Form\AccountPasswordType', []);
    $editPasswordForm->handleRequest($request);

    /**
     * Form to delete the account
     */
    $deleteForm = $this->createDeleteForm($account);

    if ($editAccountForm->isSubmitted() && $editAccountForm->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('account_edit', array('username' => $account->getUsername()));
    } else if ($editPasswordForm->isSubmitted() && $editPasswordForm->isValid()) {
        $account = $originalAccount;

        $this->getDoctrine()->getManager()->flush();
        return $this->redirectToRoute('account_edit', [
                'username' => $originalAccount->getUsername()
        ]);
    }

有什么问题?

  • 验证密码表单不起作用(两个不同的字段不会触发'字段不同')
  • 如果我提交密码字段,则
  • $ account未正确设置,导致发出错误说明查询缺少参数

我认为我这样做的方式不是很好,但我没有找到任何关于如何在同一页面上编辑同一实体的2个表单的干净文档/好发布帖。

3 个答案:

答案 0 :(得分:0)

好的,我解决了!

我做了什么:

首先,我在project/src/AppBundle/Form/中创建了两个表单:

<强> AccountGeneralDataType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class AccountGeneralDataType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /**
         * todo: Missing avatar upload
         */
        $builder
            ->add('username', TextType::class, [
                'label' => 'Username',
                'required' => true,
            ])
            ->add('email', EmailType::class, [
                'label' => 'Email',
                'required' => true,
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Save changes',
                'attr' => [
                    'class' => 'btn btn-outline-primary float-right',
                ]
            ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Account',
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_general_data_account';
    }

}

不要忘记在getBlockPrefix()

中设置唯一标签

<强> AccountPasswordType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Validator\Constraints\NotBlank;

class AccountPasswordType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('old-password', PasswordType::class, [
                'label' => 'Old password',
                'mapped' => false,
            ])
            ->add('new-password', RepeatedType::class, [
                'label' => 'New password',
                'mapped' => false,
                'type' => PasswordType::class,
                'invalid_message' => 'The new password fields must match.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => true,
                'first_options' => [
                    'label' => 'New password'
                ],
                'second_options' => [
                    'label' => 'Repeat password'
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Update password',
                'attr' => [
                    'class' => 'btn btn-outline-primary float-right'
                ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'allow_extra_fields' => true,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_change_password';
    }

}

不要忘记在getBlockPrefix()

中设置唯一标签

AccountPasswordType不使用data_class(在configureOptions()方法中设置),因为我没有使用它自动填充对象。

其次,我在我的控制器中打电话给他们:

<强> AccountController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Account;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\FormError;

/**
 * Account controller.
 *
 * @Route("account")
 */
class AccountController extends Controller
{
    /**
     * Displays forms to edit an existing account entity.
     *
     * @Route("/edit/{username}", name="account_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Account $account)
    {
        /**
         * Form to edit general information
         */
        $editAccountGeneralInformationForm = $this->createForm('AppBundle\Form\AccountGeneralDataType', $account);
        $editAccountGeneralInformationForm->handleRequest($request);

        /**
         * Form to edit password
         */
        $editAccountPasswordForm = $this->createForm('AppBundle\Form\AccountPasswordType', []);
        $editAccountPasswordForm->handleRequest($request);

        // Handle the account form
        if ($editAccountGeneralInformationForm->isSubmitted() && !$editAccountGeneralInformationForm->isValid())
        {
            $session->getFlashBag()->add('danger', 'Your iinformation have not been updated, please retry.');
        }

        if ($editAccountGeneralInformationForm->isSubmitted() && $editAccountGeneralInformationForm->isValid())
        {
            $this->getDoctrine()->getManager()->flush();

            $session->getFlashBag()->add('success', 'Your information have been updated.');
        }


        // Handle the password form
        if ($editAccountPasswordForm->isSubmitted() && !$editAccountPasswordForm->isValid())
        {
            $session->getFlashBag()->add('danger', 'Your password have not been updated, please retry.');
        }

        if ($editAccountPasswordForm->isSubmitted() && $editAccountPasswordForm->isValid())
        {
            $oldUserPassword = $account->getPassword();
            $oldPasswordSubmitted = $editAccountPasswordForm['old-password']->getData();
            $newPasswordSubmitted = $editAccountPasswordForm['new-password']->getData();

            if (password_verify($oldPasswordSubmitted, $oldUserPassword))
            {
                $newPassword = password_hash($newPasswordSubmitted, PASSWORD_BCRYPT, ['cost' => 15]);
                $account->setPassword($newPassword);
                $this->getDoctrine()->getManager()->flush();

                $session->getFlashBag()->add('success', 'Your password have been updated.');
            }
            else
            {
                $editAccountPasswordForm->get('old-password')->addError(new FormError('Incorrect password.'));
                $session->getFlashBag()->add('danger', 'Your password have not been updated, please retry.');
            }
        }

        return $this->render('account/edit.html.twig', [
                'account' => $account,
                'edit_form' => $editAccountGeneralInformationForm->createView(),
                'edit_password_form' => $editAccountPasswordForm->createView(),
        ]);
    }
}

使用$foobarForm->isSubmitted(),我们可以知道表单是否已提交。

我最大的问题是我的表单的两个Type类具有相同的名称(在getBlockPrefix()中定义),所以当我提交第二个类时,它是第一个抛出错误的类。

答案 1 :(得分:-1)

您可以使用树枝在另一个视图中渲染两个视图。

首先,在新控制器中创建一个新视图及其操作。 将此控制器路由注册到路由文件中。

然后使用它将您的两个表单视图呈现到新创建的视图

{{ render(controller('AppBundle:Controller:Action')) }}

这应该可以解决问题......;)

答案 2 :(得分:-2)

如果您要使用一个页面和两个表单,他们将在Request中分开。

例如,您必须评估提交的表单如下:

if ($request->request->has('editAccountForm')) {
    // handle the account form 
}

if ($request->request->has('editPasswordForm')) {
    // handle the password form  
}

Symfony 2的博客文章covers this。它与Symfony 3类似。