valueParser($config['type'], $param->get($name, false)); } // build the form $formBuilder = $this->createFormBuilder($data); foreach ($configuration as $config) { $this->configParser($config, $formBuilder); } $form = $formBuilder->add('Update', SubmitType::class)->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $result = $form->getData(); foreach ($configuration as $config) { $name = $config['name']; if (null !== $result[$name] || CheckboxType::class == $config['type']) { $param->set($name, $result[$name]); } } $this->addFlash('success', 'Updating parameters successful'); return $this->redirect($request->getRequestUri()); } return $this->render('@ArdentParameter/form.html.twig', [ 'parameter_form' => $form->createView(), ]); } private function valueParser($type, $value) { switch ($type) { case CheckboxType::class: return boolval($value); break; default: return $value; break; } } /** * @param $config * @param FormBuilderInterface $formBuilder */ private function configParser($config, &$formBuilder) { $type = $config['type']; $options = []; switch ($type) { case ChoiceType::class: if (is_callable($config['choices'])) { $options['choices'] = $config['choices'](); } else { $options['choices'] = $config['choices']; } break; case CheckboxType::class: $options['required'] = false; break; default: break; } $formBuilder->add($config['name'], $type, $options); } }