如何在Symfony 4中创建“更新”表单?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (151)

使用此表单我想更新我的数据库条目:

myController.php

  public function index($id, $slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
  {

      $item = new User();

      $item= $this->getDoctrine()->getRepository(User::class)->find($id);
      $form = $this->createFormBuilder($item)
      ->add('username', TextType::class, array('attr' => array('class' => 'form-control')))
      ->add('email', EmailType::class, array('attr' => array('class' => 'form-control')))
      ->add('is_active', HiddenType::class)
      ->add('plainPassword', RepeatedType::class, array('type' => PasswordType::class,'invalid_message' => 'The password fields must match.','options' => array('attr' => array('class' => 'password-field')),'required' => true,'first_options'  => array('label' => 'Passwort', 'attr' => array('class' => 'form-control')),'second_options' => array('label' => 'Passwort wiederholen', 'attr' => array('class' => 'form-control')),))
      ->add('cancel', ButtonType::class, array('label' => 'Cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar close_h')))
      ->add('update', SubmitType::class, array('label' => 'Update','attr' => array('class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))
      ->getForm();
      $form->handleRequest($request);

     if($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->flush();
            return $this->redirectToRoute('pages', array(
              'slug' => $slug,
            ));
      }
      return $this->render('form.html.twig', ['form' => $form->createView()]);

}

form.html.twig:

{{ form_start(form) }}
<div>
  {{ form_label(form.username) }}
  {{ form_widget(form.username) }}
  {{ form_errors(form.username) }}
  {{ form_help(form.username) }}
</div>
<div>
  {{ form_label(form.email) }}
  {{ form_widget(form.email) }}
  {{ form_errors(form.email) }}
  {{ form_help(form.email) }}
</div>

<div>
  {{ form_widget(form.is_active) }}
  {{ form_errors(form.is_active) }}
  {{ form_help(form.is_active) }}
</div>

<div>
  {{ form_label(form.plainPassword.first) }}
  {{ form_widget(form.plainPassword.first) }}
  <div class="text-red">{{ form_errors(form.plainPassword.first) }}</div>
  {{ form_help(form.plainPassword.first) }}
</div>
<div>
  {{ form_label(form.plainPassword.second) }}
  {{ form_widget(form.plainPassword.second) }}
  <div class="text-red">{{ form_errors(form.plainPassword.second) }}</div>
  {{ form_help(form.plainPassword.second) }}
</div>

{{ form_end(form) }}

但是,当我在我的表单中编辑用户名时,我点击“更新”,然后我收到错误消息

An exception occurred while executing 'INSERT INTO members (username, password, email, is_active) VALUES (?, ?, ?, ?)' with params ["cat2", "123", "cat@cat.de", "1"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cat@cat.de' for key 'UNIQ_12A0D2FFE7927C74'

提问于
用户回答回答于

你需要从UserRepository:find($id)加载用户:

use App\Repository\UserRepository;
// ...

class MyController
{    
    public function index($id, $slug, 
                          Request $request,
                          UserPasswordEncoderInterface $passwordEncoder,
                          UserRepository $userRepository)
    {

        $item = $userRepository->find($id);

        if(NULL === $item) {
            throw $this->createNotFoundException('could not find this user.');
        }

        $form = $this->createFormBuilder($item)

        // ...
    }

    // ...
}

扫码关注云+社区

领取腾讯云代金券