Symfony-两种形式分别激活

时间:2019-01-15 15:04:37

标签: forms symfony twig

我编写了一个函数,该函数具有分别激活的两种形式,并保留同一实体。首先激活输入字段,然后激活随机生成的字段。

但是当我单击“提交”时,它会依次激活两个表单。如果使用条件来防止这种情况出现,但我看来这是行不通的。

我的代码:

     $id = $request->get('id');

    $user = $this->container->get('account')->getUserRepository()->find($id);

    $form1 = $this->createFormBuilder()
    ->add('password', PasswordType::class, array(
        'label' => 'Enter New Password',
        'attr' => ['class'=>'form-control']))
    ->add('save', SubmitType::class, array(
        'label' => 'Send', 'attr' => ['class' => 'btn btn-primary action-save']
    ))
    ->getForm();

    $form2 = $this->createFormBuilder()
    ->add('password', PasswordType::class, array(
        'label' => 'Generate New Password',
        'disabled'=> true,
        'attr' => ['class'=>'form-control']))
    ->add('save', SubmitType::class, array(
        'label' => 'Send',
        'attr' => ['class' => 'btn btn-primary action-save']
    ))
    ->getForm();

    $form1->handleRequest($request);
    if($form1->isSubmitted() && $form1->isValid()) {

        $this->addFlash(
            'notice',
            'You successfully changed the password!'
        );

        $data = $form1->getData();

        $new_password = $data['password'];

        $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
        $new_pwd_encoded = $encoder->encodePassword($new_password);

        $oneTimePsw = '';
        $user->setPassword($new_pwd_encoded);
        $manager = $this->getDoctrine()->getManager();

        $manager->flush();
    }

    $form2->handleRequest($request);
    if($form2->isSubmitted() && $form2->isValid()) {

        $this->addFlash(
            'notice',
            'Password is successfully generated!'
        );

        $data = $form2->getData();

        $new_password = $data['password'];);

        $new = $this->get('member.account')->generateRandomPassword();

        $oneTimePsw = '';
        $user->setPassword($new);
        $manager = $this->getDoctrine()->getManager();

        $manager->flush();
    }
return $this->render('@AdminTemplates/admin/reset_password.html.twig', array(
        'form1' => $form1->createView(),
        'form2' => $form2->createView()
    ));

My twig

    <div id="setPassword" style="display:none;">
    {{ form_start(form1) }}

    {{ form_end(form1) }}
</div>

<div id="generatePassword" style="display:none;">
    {{ form_start(form2) }}

    {{ form_end(form2) }}
</div>

1 个答案:

答案 0 :(得分:0)

我认为您的问题很简单,如果不是其他问题,当我遇到同样的问题时,这就是我所做的事情:

$id = $request->get('id');

$user = $this->container->get('account')->getUserRepository()->find($id);

$form1 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
    'label' => 'Enter New Password',
    'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
    'label' => 'Send', 'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();

$form2 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
    'label' => 'Generate New Password',
    'disabled'=> true,
    'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
    'label' => 'Send',
    'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();
$form1->handleRequest($request);
$form2->handleRequest($request);
if($form1->isSubmitted() && $form1->isValid()) {

    $this->addFlash(
        'notice',
        'You successfully changed the password!'
    );

    $data = $form1->getData();

    $new_password = $data['password'];

    $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
    $new_pwd_encoded = $encoder->encodePassword($new_password);

    $oneTimePsw = '';
    $user->setPassword($new_pwd_encoded);
    $manager = $this->getDoctrine()->getManager();

    $manager->flush();
}else

    if($form2->isSubmitted() && $form2->isValid()) {

    $this->addFlash(
        'notice',
        'Password is successfully generated!'
    );

    $data = $form2->getData();

    $new_password = $data['password'];);

    $new = $this->get('member.account')->generateRandomPassword();

    $oneTimePsw = '';
    $user->setPassword($new);
    $manager = $this->getDoctrine()->getManager();

    $manager->flush();
}
return $this->render('@AdminTemplates/admin/reset_password.html.twig', array(
    'form1' => $form1->createView(),
    'form2' => $form2->createView()
));

这应该可行,因为用户不能一次提交2个表单。我的isValid()方法也有问题,因此也请尝试进行测试以清除它。