| 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: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class StringType extends BaseType |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public function configureOptions(OptionsResolver $resolver): void |
| 26: | { |
| 27: | parent::configureOptions($resolver); |
| 28: | |
| 29: | $resolver->setDefaults(array( |
| 30: | 'compound' => false, |
| 31: | 'type' => null, |
| 32: | 'always_empty' => null, |
| 33: | 'mailto' => null, |
| 34: | 'linkto' => null, |
| 35: | 'default_protocol' => 'http', |
| 36: | )); |
| 37: | |
| 38: | $resolver->setAllowedTypes('type', array('null', 'string')); |
| 39: | $resolver->setAllowedTypes('always_empty', array('null', 'bool')); |
| 40: | $resolver->setAllowedTypes('mailto', array('null', 'string', 'bool')); |
| 41: | $resolver->setAllowedTypes('linkto', array('null', 'string', 'bool')); |
| 42: | |
| 43: | $resolver->setAllowedValues('type', array(null, 'text', 'password', 'email', 'phone', 'tel', 'url')); |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function buildView(FormView $view, FormInterface $form, array $options): void |
| 63: | { |
| 64: | switch (true) { |
| 65: | case ($options['type'] === 'url' || ! empty($options['linkto'])) : |
| 66: | $this->default_prefix = '<globe>'; |
| 67: | if ($options['placeholder'] === 'format') $options['placeholder'] = 'ticform.string.ph_link'; |
| 68: | if ($options['pattern'] === true) $options['pattern'] = '^https?:\/\/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,8}$'; |
| 69: | if (! empty($options['linkto'])) { |
| 70: | $view->vars['linkto'] = is_bool($options['linkto']) ? 'ticform.string.linkto' : $options['linkto']; |
| 71: | $view->vars['input_group'] = true; |
| 72: | } |
| 73: | break; |
| 74: | case ($options['type'] === 'email' || ! empty($options['mailto'])) : |
| 75: | $this->default_prefix = '<envelope>'; |
| 76: | if ($options['placeholder'] === 'format') $options['placeholder'] = 'ticform.string.ph_mail'; |
| 77: | if ($options['pattern'] === true) { $options['pattern'] = null; $options['type'] = 'email'; } |
| 78: | if (! empty($options['mailto'])) { |
| 79: | $view->vars['mailto'] = is_bool($options['mailto']) ? 'ticform.string.mailto' : $options['mailto']; |
| 80: | $view->vars['input_group'] = true; |
| 81: | } |
| 82: | break; |
| 83: | case ($options['type'] === 'phone' || $options['type'] === 'tel') : |
| 84: | $this->default_prefix = '<telephone>'; |
| 85: | if ($options['pattern'] === true) $options['pattern'] = '^(?:0|\(?\+33\)?\s?|0033\s?)[1-79](?:[\.\-\s]?\d\d){4}$'; |
| 86: | if ($options['placeholder'] === 'format') $options['placeholder'] = 'ticform.string.ph_ntel'; |
| 87: | $options['type'] = 'tel'; |
| 88: | break; |
| 89: | } |
| 90: | |
| 91: | parent::buildView($view, $form, $options); |
| 92: | |
| 93: | |
| 94: | $this->setViewInputSizing($view); |
| 95: | |
| 96: | |
| 97: | if ($options['type'] === null) { |
| 98: | if (! empty($options['mailto'])) |
| 99: | $options['type'] = 'email'; |
| 100: | else |
| 101: | $options['type'] = 'text'; |
| 102: | } |
| 103: | $view->vars['type'] = $options['type']; |
| 104: | |
| 105: | |
| 106: | if ($options['always_empty'] === null) { |
| 107: | $options['always_empty'] = ($options['type'] === 'password') ? true : false; |
| 108: | } |
| 109: | if ($options['always_empty']) $view->vars['value'] = ''; |
| 110: | |
| 111: | |
| 112: | if (! $options['strict'] && ! is_string($view->vars['value'])) { |
| 113: | $value = $view->vars['value']; |
| 114: | if ($value === null) $value = ''; |
| 115: | elseif (is_bool($value)) $value = ($value) ? '1' : '0'; |
| 116: | |
| 117: | elseif (is_array($value)) $value = reset($value); |
| 118: | else $value = (string)$value; |
| 119: | $view->vars['value'] = $value; |
| 120: | } |
| 121: | } |
| 122: | |
| 123: | |
| 124: | } |
| 125: | |