Add support for checkboxtype (booleans)

This commit is contained in:
Tim 2019-06-03 11:29:44 +02:00
parent 301862589b
commit 03b561ab1d

View File

@ -4,6 +4,7 @@ namespace App\Ardent\ParameterBundle\Controller;
use App\Ardent\ParameterBundle\Service\ParameterService; use App\Ardent\ParameterBundle\Service\ParameterService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 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\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -22,7 +23,7 @@ abstract class BaseController extends AbstractController
$data = []; $data = [];
foreach ($configuration as $config) { foreach ($configuration as $config) {
$name = $config['name']; $name = $config['name'];
$data[$name] = $param->get($name, false); $data[$name] = $this->valueParser($config['type'], $param->get($name, false));
} }
// build the form // build the form
@ -39,10 +40,8 @@ abstract class BaseController extends AbstractController
foreach ($configuration as $config) { foreach ($configuration as $config) {
$name = $config['name']; $name = $config['name'];
if ($result[$name]) {
$param->set($name, $result[$name]); $param->set($name, $result[$name]);
} }
}
$this->addFlash('success', 'Updating parameters successful'); $this->addFlash('success', 'Updating parameters successful');
return $this->redirect($request->getRequestUri()); return $this->redirect($request->getRequestUri());
@ -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 $config
* @param FormBuilderInterface $formBuilder * @param FormBuilderInterface $formBuilder
@ -65,6 +76,9 @@ abstract class BaseController extends AbstractController
case ChoiceType::class: case ChoiceType::class:
$options['choices'] = $config['choices']; $options['choices'] = $config['choices'];
break; break;
case CheckboxType::class:
$options['required'] = false;
break;
default: default:
break; break;
} }