| 1: | <?php |
| 2: | namespace TIC\FormBundle\Form\Type; |
| 3: | |
| 4: | |
| 5: | use TIC\FormBundle\Base\TICWidgetType as BaseType; |
| 6: | |
| 7: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 8: | use Symfony\Component\Form\FormBuilderInterface; |
| 9: | use Symfony\Component\Form\FormInterface; |
| 10: | use Symfony\Component\Form\FormView; |
| 11: | |
| 12: | use TIC\FormBundle\Form\DataTransformer\BooleanOrNullTransformer; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | class BooleanType extends BaseType |
| 19: | { |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function configureOptions(OptionsResolver $resolver): void |
| 27: | { |
| 28: | parent::configureOptions($resolver); |
| 29: | |
| 30: | $resolver->setDefaults(array( |
| 31: | 'compound' => false, |
| 32: | 'expanded' => true, |
| 33: | 'nullable' => false, |
| 34: | 'with_js' => false, |
| 35: | 'labels' => array( |
| 36: | 'ticform.boolean.false', |
| 37: | 'ticform.boolean.true', |
| 38: | ), |
| 39: | 'choice_attr' => array(), |
| 40: | )); |
| 41: | $resolver->setAllowedTypes('expanded', array('bool')); |
| 42: | $resolver->setAllowedTypes('nullable', array('bool')); |
| 43: | $resolver->setAllowedTypes('with_js', array('bool')); |
| 44: | $resolver->setAllowedTypes('labels', array('string', 'array')); |
| 45: | $resolver->setAllowedTypes('choice_attr', array('array')); |
| 46: | } |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | public function buildForm(FormBuilderInterface $builder, array $options): void |
| 53: | { |
| 54: | $builder->addModelTransformer(new BooleanOrNullTransformer( ! $options['nullable'] )); |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function buildView(FormView $view, FormInterface $form, array $options): void |
| 62: | { |
| 63: | parent::buildView($view, $form, $options); |
| 64: | |
| 65: | if (! is_array($options['labels'])) $options['labels'] = explode('|', $options['labels']); |
| 66: | if (count($options['labels']) > 1) { |
| 67: | $view->vars['label_0'] = $options['labels'][0]; |
| 68: | $view->vars['label_1'] = $options['labels'][1]; |
| 69: | } else { |
| 70: | $view->vars['label_0'] = ''; |
| 71: | $view->vars['label_1'] = reset($options['labels']); |
| 72: | } |
| 73: | |
| 74: | $view->vars['expanded'] = empty($options['expanded']) ? false : true; |
| 75: | $view->vars['with_js'] = empty($options['with_js']) ? false : true; |
| 76: | $view->vars['choice_attr'] = $options['choice_attr']; |
| 77: | } |
| 78: | |
| 79: | |
| 80: | } |
| 81: | |