From 03b561ab1d28a63db5a5a0be433036107e4eeea0 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 3 Jun 2019 11:29:44 +0200 Subject: [PATCH] Add support for checkboxtype (booleans) --- Controller/BaseController.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Controller/BaseController.php b/Controller/BaseController.php index 11c4fc9..b6244da 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -4,6 +4,7 @@ namespace App\Ardent\ParameterBundle\Controller; use App\Ardent\ParameterBundle\Service\ParameterService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormBuilderInterface; @@ -22,7 +23,7 @@ abstract class BaseController extends AbstractController $data = []; foreach ($configuration as $config) { $name = $config['name']; - $data[$name] = $param->get($name, false); + $data[$name] = $this->valueParser($config['type'], $param->get($name, false)); } // build the form @@ -39,9 +40,7 @@ abstract class BaseController extends AbstractController foreach ($configuration as $config) { $name = $config['name']; - if ($result[$name]) { - $param->set($name, $result[$name]); - } + $param->set($name, $result[$name]); } $this->addFlash('success', 'Updating parameters successful'); @@ -53,6 +52,18 @@ abstract class BaseController extends AbstractController ]); } + private function valueParser($type, $value) + { + switch ($type) { + case CheckboxType::class: + return boolval($value); + break; + default: + return $value; + break; + } + } + /** * @param $config * @param FormBuilderInterface $formBuilder @@ -65,6 +76,9 @@ abstract class BaseController extends AbstractController case ChoiceType::class: $options['choices'] = $config['choices']; break; + case CheckboxType::class: + $options['required'] = false; + break; default: break; }