I have updated from Symfony 3.x to Symfony 4.4.16 and verified the operation.
When I press “Registration/change” to save the form, I am redirected to the form input screen.
What I want to do is save the entered information in the DB and display the top screen and success message.
Are there any changes you made?
When I tried php bin/console router:match /admin/initialSetting --method=PUT
, it didn’t seem to be a problem.
Controller
/* * @Method("GET") * @Route("/initialSetting") * @Template("@AppBundle/Security/initialSetting.html.twig") */ public function initialSettingAction(Request $request) { $key = $request->query->get("key"); $staff = $this->get("admin.staffService")->getStaffByUrlKey($key); // Create form $form = $this->createForm(InitialSettingType::class, $staff, array( "method" => "PUT", "action" => $this->generateUrl("app_security_initialsetting", array("key" => $key)), )); // Show screen return array( 'form' => $form->createView(), ); } /* * @Method("PUT") * @Route("/initialSetting") * @Template("@AppBundle/Security/initialSetting.html.twig") */ public function updateInitialSettingAction(Request $request) { $key = $request->query->get("key"); $staff = $this->get("admin.staffService")->getStaffByUrlKey($key); // Create Form $form = $this->createForm(InitialSettingType::class, $staff, array( "method" => "PUT", "action" => $this->generateUrl("app_security_updateinitialsetting", array("key" => $key)), )); // Request processing if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) { // URL key clear, save $this->get("admin.staffService")->clearUrlKey($staff); $this->get("admin.staffService")->save($staff); $this->get('session')->getFlashBag()->add('success', 'Staff profile Initial settings have been completed.'); // Login $this->autoLogin($staff); return $this->redirect($this->generateUrl('app_admin_default_index')); } else { $this->get('session')->getFlashBag()->add('error', 'Staff profile Initial settings could not be completed. Please check the input contents.'); } // Show screen return array( 'form' => $form->createView(), ); }
services.yaml
app.form.type.initialSettingType: class: AppBundleFormTypeStaffInitialSettingType arguments: [ "@service_container", "@admin.staff" ] tags: - { name: form.type } admin.staff: class: AppBundleModelEntityStaff
initialSetting.html.twig
<div class="formGroup"> {{ form_widget(form.submit, {label: "Registration/change", attr: {class: "btn"}}) }} </div>
Postscript
Looking at the code in the browser’s development tools, I found that POST specified the method of the form.
Is there any lack of package updates?
<form name="initialSetting" method="post" action="/url/url/" novalidate="novalidate" autocomplete="off"> <input type="hidden" name="_method" value="PUT">
Version
sensio/framework-extra-bundle v5.2.4
symfony/form v4.4.19
Advertisement
Answer
I changed it to the code below and solved the problem.
/** * * @Method("GET") * @Route("/initialSetting") * * @Template("@AhiSpAdminBundle/Security/initialSetting.html.twig") */ public function initialSettingAction(Request $request) { return $this->initialSettingAndUpdateAction($request); } /** * * @Method("PUT") * @Route("/initialSetting") * * @Template("@AhiSpAdminBundle/Security/initialSetting.html.twig") */ public function updateAction(Request $request) { return $this->initialSettingAndUpdateAction($request); } public function initialSettingAndUpdateAction(Request $request) { $key = $request->query->get("key"); $staff = $this->get("admin.staffService")->getStaffByUrlKey($key); if (!$staff || $staff->getPassword()) { throw new HttpException( 400, The specified stuff prowl initialization URL does not exist or has expired. "n". "Ask the administrator to reissue the URL." ); } $form = $this->createForm(InitialSettingType::class, $staff, array( "method" => "PUT", "action" => $this->generateUrl("ahi_sp_admin_security_update", array("key" => $key)), )); if ($request->isMethod('PUT')) { if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) { $this->get("admin.staffService")->clearUrlKey($staff); $this->get("admin.staffService")->save($staff); $this->get('session')->getFlashBag()->add('success', 'Staff profile Initial settings have been completed.'); $this->autoLogin($staff); return $this->redirect($this->generateUrl('ahi_sp_admin_default_index')); } else { $this->get('session')->getFlashBag()->add('error', 'Staff profile Initial settings could not be completed. Please check the input contents.'); } } return array( 'form' => $form->createView(), ); }