1: <?php
2: namespace TIC\FormBundle\Form\Type;
3:
4: #use Symfony\Component\Form\AbstractType as BaseType;
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: * Saisie d'une valeur textuelle avec options supplémentaires
14: * (affichage de prefixe/suffixe + bouton mailto/linkto + placeholder).
15: * Remplace : TextType, PasswordType, UrlType, EmailType, TelType
16: */
17: class StringType extends BaseType
18: {
19: # protected $form_parent = \Symfony\Component\Form\Extension\Core\Type\TextType::class;
20:
21:
22: /**
23: * {@inheritdoc}
24: */
25: public function configureOptions(OptionsResolver $resolver): void
26: {
27: parent::configureOptions($resolver);
28:
29: $resolver->setDefaults(array(
30: 'compound' => false,
31: 'type' => null, // type du tag input ('text' par défaut, 'email' si mailto, 'tel|phone', 'password'...)
32: 'always_empty' => null, // activé par défaut avec un type password
33: 'mailto' => null, // valeurs : false/null ou true/label
34: 'linkto' => null, // valeurs : false/null ou true/label
35: 'default_protocol' => 'http', // équivalent à UrlType quand linkto activé
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: * {@inheritdoc}
49: public function buildForm(FormBuilderInterface $builder, array $options): void
50: {
51: // reprise du code de UrlType lorsque linkto est activé
52: if ($options['linkto'] && $options['default_protocol'] !== null) {
53: $builder->addEventSubscriber(new \Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener($options['default_protocol']));
54: }
55: }
56: */
57:
58:
59: /**
60: * {@inheritdoc}
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'; } // laisser faire le navigateur !
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}$'; // france
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: // spécification de la taille du widget (ajout de classe bootstrap)
94: $this->setViewInputSizing($view);
95:
96: // détermination du type par défaut ('text' ou 'email' si option 'mailto')
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: // gestion de l'option 'always_empty' (activation automatique avec le type password)
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: // tolérance sur un format de data source (en bdd) de type invalide (conversion auto au mieux)
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: # elseif (is_int($value)) $value = "$value";
117: elseif (is_array($value)) $value = reset($value);
118: else $value = (string)$value;
119: $view->vars['value'] = $value;
120: }
121: }
122:
123:
124: }
125: