我想要一个包含两个表单的页面
这两种形式更新同一个实体:帐户。
我做了什么:
- 创建了两个表单(
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 设置不正确,导致 Doctrine 错误提示查询缺少参数
我认为我的做法不是很好,但我没有找到任何关于如何让 2 个表单在同一页面上编辑同一实体的干净文档/好的开发帖子。