/
Widget.php
1568 lines (1342 loc) · 39.2 KB
/
Widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\Database\Result;
use Doctrine\DBAL\Types\Types;
/**
* Generates and validates form fields
*
* The class functions as abstract parent class for all widget classes and
* provides methods to generate the form field markup and to validate the form
* field input.
*
* Usage:
*
* $widget = new TextField();
* $widget->name = 'test';
* $widget->label = 'Test';
*
* if ($_POST)
* {
* $widget->validate();
*
* if (!$widget->hasErrors())
* {
* echo $widget->value;
* }
* }
*
* @property string $id The field ID
* @property string $name the field name
* @property string $label The field label
* @property mixed $value The field value
* @property string $class One or more CSS classes
* @property string $prefix The CSS class prefix
* @property string $template The template name
* @property string $wizard The field wizard markup
* @property string $alt The alternative text
* @property string $style The style attribute
* @property string $accesskey The key to focus the field
* @property integer $tabindex The tabindex of the field
* @property boolean $disabled Adds the disabled attribute
* @property boolean $readonly Adds the readonly attribute
* @property boolean $autofocus Adds the autofocus attribute
* @property boolean $required Adds the required attribute
* @property string $onblur The blur event
* @property string $onchange The change event
* @property string $onclick The click event
* @property string $ondblclick The double click event
* @property string $onfocus The focus event
* @property string $onmousedown The mouse down event
* @property string $onmousemove The mouse move event
* @property string $onmouseout The mouse out event
* @property string $onmouseover The mouse over event
* @property string $onmouseup The mouse up event
* @property string $onkeydown The key down event
* @property string $onkeypress The key press event
* @property string $onkeyup The key up event
* @property string $onselect The select event
* @property boolean $mandatory The field value must not be empty
* @property boolean $nospace Do not allow whitespace characters
* @property boolean $allowHtml Allow HTML tags in the field value
* @property boolean $storeFile Store uploaded files in a given folder
* @property boolean $useHomeDir Store uploaded files in the user's home directory
* @property boolean $trailingSlash Add or remove a trailing slash
* @property boolean $spaceToUnderscore Convert spaces to underscores
* @property boolean $doNotTrim Do not trim the user input
* @property string $forAttribute The "for" attribute
* @property DataContainer $dataContainer The data container object
* @property Result $activeRecord The active record
* @property string $mandatoryField The "mandatory field" label
* @property string $customTpl A custom template name
* @property string $slabel The submit button label
* @property boolean $preserveTags Preserve HTML tags
* @property boolean $decodeEntities Decode HTML entities
* @property boolean $useRawRequestData Use the raw request data from the Symfony request
* @property integer $minlength The minimum length
* @property integer $maxlength The maximum length
* @property integer $minval The minimum value
* @property integer $maxval The maximum value
* @property integer $rgxp The regular expression name
* @property boolean $isHexColor The field value is a hex color
* @property string $strTable The table name
* @property string $strField The field name
* @property string $xlabel
* @property string $customRgxp
* @property string $errorMsg
* @property integer $currentRecord
* @property integer $rowClass
* @property integer $rowClassConfirm
* @property integer $storeValues
* @property boolean $includeBlankOption
* @property string $blankOptionLabel
*/
abstract class Widget extends Controller
{
use TemplateInheritance;
/**
* Id
* @var integer
*/
protected $strId;
/**
* Name
* @var string
*/
protected $strName;
/**
* Label
* @var string
*/
protected $strLabel;
/**
* Value
* @var mixed
*/
protected $varValue;
/**
* Input callback
* @var callable
*/
protected $inputCallback;
/**
* CSS class
* @var string
*/
protected $strClass;
/**
* CSS class prefix
* @var string
*/
protected $strPrefix;
/**
* Wizard
* @var string
*/
protected $strWizard;
/**
* Errors
* @var array
*/
protected $arrErrors = array();
/**
* Attributes
* @var array
*/
protected $arrAttributes = array();
/**
* Configuration
* @var array
*/
protected $arrConfiguration = array();
/**
* Options
* @var array
*/
protected $arrOptions = array();
/**
* Submit indicator
* @var boolean
*/
protected $blnSubmitInput = false;
/**
* For attribute indicator
* @var boolean
*/
protected $blnForAttribute = false;
/**
* Data container
* @var object
*/
protected $objDca;
/**
* Initialize the object
*
* @param array $arrAttributes An optional attributes array
*/
public function __construct($arrAttributes=null)
{
parent::__construct();
$this->addAttributes($arrAttributes);
}
/**
* Set an object property
*
* @param string $strKey The property name
* @param mixed $varValue The property value
*/
public function __set($strKey, $varValue)
{
switch ($strKey)
{
case 'id':
$this->strId = $varValue;
break;
case 'name':
$this->strName = $varValue;
break;
case 'label':
$this->strLabel = $varValue;
break;
case 'value':
$this->varValue = StringUtil::deserialize($varValue);
// Decrypt the value if it is encrypted
if ($this->arrConfiguration['encrypt'] ?? null)
{
$this->varValue = Encryption::decrypt($this->varValue);
}
break;
case 'class':
if ($varValue && strpos($this->strClass ?? '', $varValue) === false)
{
$this->strClass = trim($this->strClass . ' ' . $varValue);
}
break;
case 'prefix':
$this->strPrefix = $varValue;
break;
case 'template':
$this->strTemplate = $varValue;
break;
case 'wizard':
$this->strWizard = $varValue;
break;
case 'autocomplete':
case 'autocorrect':
case 'autocapitalize':
case 'spellcheck':
if (\is_bool($varValue))
{
$varValue = $varValue ? 'on' : 'off';
}
// no break
case 'alt':
case 'style':
case 'accesskey':
case 'onblur':
case 'onchange':
case 'onclick':
case 'ondblclick':
case 'onfocus':
case 'onmousedown':
case 'onmousemove':
case 'onmouseout':
case 'onmouseover':
case 'onmouseup':
case 'onkeydown':
case 'onkeypress':
case 'onkeyup':
case 'onselect':
$this->arrAttributes[$strKey] = $varValue;
break;
case 'tabindex':
if ($varValue > 0)
{
trigger_deprecation('contao/core-bundle', '4.12', 'Using a tabindex value greater than 0 has been deprecated and will no longer work in Contao 5.0.');
$this->arrAttributes['tabindex'] = $varValue;
}
break;
case 'disabled':
case 'readonly':
$this->blnSubmitInput = $varValue ? false : true;
// no break
case 'autofocus':
if ($varValue)
{
$this->arrAttributes[$strKey] = $strKey;
}
else
{
unset($this->arrAttributes[$strKey]);
}
break;
case 'required':
if ($varValue)
{
$this->strClass = trim($this->strClass . ' mandatory');
}
// no break
case 'mandatory':
case 'nospace':
case 'allowHtml':
case 'storeFile':
case 'useHomeDir':
case 'storeValues':
case 'trailingSlash':
case 'spaceToUnderscore':
case 'doNotTrim':
case 'useRawRequestData':
$this->arrConfiguration[$strKey] = $varValue ? true : false;
break;
case 'forAttribute':
$this->blnForAttribute = $varValue;
break;
case 'dataContainer':
$this->objDca = $varValue;
break;
case strncmp($strKey, 'ng-', 3) === 0:
case strncmp($strKey, 'data-', 5) === 0:
$this->arrAttributes[$strKey] = $varValue;
break;
default:
$this->arrConfiguration[$strKey] = $varValue;
break;
}
}
/**
* Return an object property
*
* @param string $strKey The property name
*
* @return string The property value
*/
public function __get($strKey)
{
switch ($strKey)
{
case 'id':
return $this->strId;
case 'name':
return $this->strName;
case 'label':
return $this->strLabel;
case 'value':
// Encrypt the value
if (isset($this->arrConfiguration['encrypt']) && $this->arrConfiguration['encrypt'])
{
return Encryption::encrypt($this->varValue);
}
if ($this->varValue === '')
{
return $this->getEmptyStringOrNull();
}
return $this->varValue;
case 'class':
return $this->strClass;
case 'prefix':
return $this->strPrefix;
case 'template':
return $this->strTemplate;
case 'wizard':
return $this->strWizard;
case 'required':
return $this->arrConfiguration[$strKey];
case 'forAttribute':
return $this->blnForAttribute;
case 'dataContainer':
return $this->objDca;
case 'activeRecord':
return $this->objDca->activeRecord;
default:
if (isset($this->arrAttributes[$strKey]))
{
return $this->arrAttributes[$strKey];
}
if (isset($this->arrConfiguration[$strKey]))
{
return $this->arrConfiguration[$strKey];
}
break;
}
return parent::__get($strKey);
}
/**
* Check whether an object property exists
*
* @param string $strKey The property name
*
* @return boolean True if the property exists
*/
public function __isset($strKey)
{
switch ($strKey)
{
case 'id':
return isset($this->strId);
case 'name':
return isset($this->strName);
case 'label':
return isset($this->strLabel);
case 'value':
return isset($this->varValue);
case 'class':
return isset($this->strClass);
case 'template':
return isset($this->strTemplate);
case 'wizard':
return isset($this->strWizard);
case 'required':
return isset($this->arrConfiguration[$strKey]);
case 'forAttribute':
return isset($this->blnForAttribute);
case 'dataContainer':
return isset($this->objDca);
case 'activeRecord':
return isset($this->objDca->activeRecord);
default:
return isset($this->arrAttributes[$strKey]) || isset($this->arrConfiguration[$strKey]);
}
}
/**
* Add an attribute
*
* @param string $strName The attribute name
* @param mixed $varValue The attribute value
*/
public function addAttribute($strName, $varValue)
{
$this->arrAttributes[$strName] = $varValue;
}
/**
* Add an error message
*
* @param string $strError The error message
*/
public function addError($strError)
{
$this->class = 'error';
$this->arrErrors[] = $strError;
}
/**
* Return true if the widget has errors
*
* @return boolean True if there are errors
*/
public function hasErrors()
{
return !empty($this->arrErrors);
}
/**
* Return the errors array
*
* @return array An array of error messages
*/
public function getErrors()
{
return $this->arrErrors;
}
/**
* Return a particular error as string
*
* @param integer $intIndex The message index
*
* @return string The corresponding error message
*/
public function getErrorAsString($intIndex=0)
{
return $this->arrErrors[$intIndex];
}
/**
* Return all errors as string separated by a given separator
*
* @param string $strSeparator An optional separator (defaults to "<br>")
*
* @return string The error messages string
*/
public function getErrorsAsString($strSeparator=null)
{
if ($strSeparator === null)
{
$strSeparator = '<br' . $this->strTagEnding . "\n";
}
return $this->hasErrors() ? implode($strSeparator, $this->arrErrors) : '';
}
/**
* Return a particular error as HTML string
*
* @param integer $intIndex The message index
*
* @return string The HTML markup of the corresponding error message
*/
public function getErrorAsHTML($intIndex=0)
{
return $this->hasErrors() ? sprintf('<p class="%s">%s</p>', ((TL_MODE == 'BE') ? 'tl_error tl_tip' : 'error'), $this->arrErrors[$intIndex]) : '';
}
/**
* Return true if the widgets submits user input
*
* @return boolean True if the widget submits user input
*/
public function submitInput()
{
return $this->blnSubmitInput;
}
/**
* Parse the template file and return it as string
*
* @param array $arrAttributes An optional attributes array
*
* @return string The template markup
*/
public function parse($arrAttributes=null)
{
if (!$this->strTemplate)
{
return '';
}
$this->addAttributes($arrAttributes);
$this->mandatoryField = $GLOBALS['TL_LANG']['MSC']['mandatory'];
if ($this->customTpl)
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
// Use the custom template unless it is a back end request
if (!$request || !System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$this->strTemplate = $this->customTpl;
}
}
$strBuffer = $this->inherit();
// HOOK: add custom parse filters (see #5553)
if (isset($GLOBALS['TL_HOOKS']['parseWidget']) && \is_array($GLOBALS['TL_HOOKS']['parseWidget']))
{
foreach ($GLOBALS['TL_HOOKS']['parseWidget'] as $callback)
{
$this->import($callback[0]);
$strBuffer = $this->{$callback[0]}->{$callback[1]}($strBuffer, $this);
}
}
return $strBuffer;
}
/**
* Generate the label and return it as string
*
* @return string The label markup
*/
public function generateLabel()
{
if (!$this->strLabel)
{
return '';
}
return sprintf(
'<label%s%s>%s%s%s</label>',
($this->blnForAttribute ? ' for="ctrl_' . $this->strId . '"' : ''),
($this->strClass ? ' class="' . $this->strClass . '"' : ''),
($this->mandatory ? '<span class="invisible">' . $GLOBALS['TL_LANG']['MSC']['mandatory'] . ' </span>' : ''),
$this->strLabel,
($this->mandatory ? '<span class="mandatory">*</span>' : '')
);
}
/**
* Generate the widget and return it as string
*
* @return string The widget markup
*/
abstract public function generate();
/**
* Generate the widget with error message and return it as string
*
* @param boolean $blnSwitchOrder If true, the error message will be shown below the field
*
* @return string The form field markup
*/
public function generateWithError($blnSwitchOrder=false)
{
$strWidget = $this->generate();
$strError = $this->getErrorAsHTML();
return $blnSwitchOrder ? $strWidget . $strError : $strError . $strWidget;
}
/**
* Return all attributes as string
*
* @param array $arrStrip An optional array with attributes to strip
*
* @return string The attributes string
*/
public function getAttributes($arrStrip=array())
{
$strAttributes = '';
foreach (array_keys($this->arrAttributes) as $strKey)
{
if (!\in_array($strKey, $arrStrip))
{
$strAttributes .= $this->getAttribute($strKey);
}
}
return $strAttributes;
}
/**
* Return a single attribute
*
* @param string $strKey The attribute name
*
* @return string The attribute markup
*/
public function getAttribute($strKey)
{
if (!isset($this->arrAttributes[$strKey]))
{
return '';
}
$varValue = $this->arrAttributes[$strKey];
// Prevent the autofocus attribute from being added multiple times (see #8281)
if ($strKey == 'autofocus')
{
unset($this->arrAttributes[$strKey]);
}
if ($strKey == 'disabled' || $strKey == 'readonly' || $strKey == 'required' || $strKey == 'autofocus' || $strKey == 'multiple')
{
return ' ' . $strKey;
}
if ('' !== (string) $varValue)
{
return ' ' . $strKey . '="' . StringUtil::specialchars($varValue) . '"';
}
return '';
}
/**
* Set a callback to fetch the widget input instead of using getPost()
*
* @param callable|null $callback The callback
*
* @return $this The widget object
*/
public function setInputCallback(callable $callback=null)
{
$this->inputCallback = $callback;
return $this;
}
/**
* Validate the user input and set the value
*/
public function validate()
{
$varValue = $this->validator($this->getPost($this->strName));
if ($this->hasErrors())
{
$this->class = 'error';
}
$this->varValue = $varValue;
}
/**
* Find and return a $_POST variable
*
* @param string $strKey The variable name
*
* @return mixed The variable value
*/
protected function getPost($strKey)
{
if (\is_callable($this->inputCallback))
{
return ($this->inputCallback)();
}
if ($this->useRawRequestData === true)
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
return $request->request->get($strKey);
}
$strMethod = $this->allowHtml ? 'postHtml' : 'post';
if ($this->preserveTags)
{
$strMethod = 'postRaw';
}
// Support arrays (thanks to Andreas Schempp)
$arrParts = explode('[', str_replace(']', '', (string) $strKey));
$varValue = Input::$strMethod(array_shift($arrParts), $this->decodeEntities);
foreach ($arrParts as $part)
{
if (!\is_array($varValue))
{
break;
}
$varValue = $varValue[$part] ?? null;
}
return $varValue;
}
/**
* Recursively validate an input variable
*
* @param mixed $varInput The user input
*
* @return mixed The original or modified user input
*/
protected function validator($varInput)
{
if (\is_array($varInput))
{
foreach ($varInput as $k=>$v)
{
$varInput[$k] = $this->validator($v);
}
return $varInput;
}
if (!$this->doNotTrim)
{
$varInput = trim($varInput);
}
if ((string) $varInput === '')
{
if (!$this->mandatory)
{
return '';
}
if (!$this->strLabel)
{
$this->addError($GLOBALS['TL_LANG']['ERR']['mdtryNoLabel']);
}
else
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['mandatory'], $this->strLabel));
}
}
if ($this->minlength && $varInput && mb_strlen($varInput) < $this->minlength)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['minlength'], $this->strLabel, $this->minlength));
}
if ($this->maxlength && $varInput && mb_strlen($varInput) > $this->maxlength)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['maxlength'], $this->strLabel, $this->maxlength));
}
if ($this->minval && is_numeric($varInput) && $varInput < $this->minval)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['minval'], $this->strLabel, $this->minval));
}
if ($this->maxval && is_numeric($varInput) && $varInput > $this->maxval)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['maxval'], $this->strLabel, $this->maxval));
}
if ($this->rgxp)
{
switch ($this->rgxp)
{
case strncmp($this->rgxp, 'digit_', 6) === 0:
// Special validation rule for style sheets
$textual = explode('_', $this->rgxp);
array_shift($textual);
if (\in_array($varInput, $textual) || strncmp($varInput, '$', 1) === 0)
{
break;
}
// no break
case 'digit':
// Support decimal commas and convert them automatically (see #3488)
if (substr_count($varInput, ',') == 1 && strpos($varInput, '.') === false)
{
$varInput = str_replace(',', '.', $varInput);
}
if (!Validator::isNumeric($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['digit'], $this->strLabel));
}
break;
case 'natural':
if (!Validator::isNatural($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['natural'], $this->strLabel));
}
break;
case 'alpha':
if (!Validator::isAlphabetic($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alpha'], $this->strLabel));
}
break;
case 'alnum':
if (!Validator::isAlphanumeric($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alnum'], $this->strLabel));
}
break;
case 'extnd':
if (!Validator::isExtendedAlphanumeric(html_entity_decode($varInput)))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['extnd'], $this->strLabel));
}
break;
case 'date':
if (!Validator::isDate($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['date'], Date::getInputFormat(Date::getNumericDateFormat())));
}
else
{
// Validate the date (see #5086)
try
{
new Date($varInput, Date::getNumericDateFormat());
}
catch (\OutOfBoundsException $e)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varInput));
}
}
break;
case 'time':
if (!Validator::isTime($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['time'], Date::getInputFormat(Date::getNumericTimeFormat())));
}
break;
case 'datim':
if (!Validator::isDatim($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['dateTime'], Date::getInputFormat(Date::getNumericDatimFormat())));
}
else
{
// Validate the date (see #5086)
try
{
new Date($varInput, Date::getNumericDatimFormat());
}
catch (\OutOfBoundsException $e)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varInput));
}
}
break;
case 'friendly':
list ($strName, $varInput) = StringUtil::splitFriendlyEmail($varInput);
// no break
case 'email':
if (!Validator::isEmail($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['email'], $this->strLabel));
}
if ($this->rgxp == 'friendly' && !empty($strName))
{
$varInput = $strName . ' [' . $varInput . ']';
}
break;
case 'emails':
// Check whether the current value is list of valid e-mail addresses
$arrEmails = StringUtil::trimsplit(',', $varInput);
foreach ($arrEmails as $strEmail)
{
$strEmail = Idna::encodeEmail($strEmail);
if (!Validator::isEmail($strEmail))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['emails'], $this->strLabel));
break;
}
}
break;
case 'url':
$varInput = StringUtil::specialcharsUrl($varInput);
if ($this->decodeEntities)
{
$varInput = StringUtil::decodeEntities($varInput);
}
if (!Validator::isUrl($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['url'], $this->strLabel));
}
break;
case 'alias':
if (!Validator::isAlias($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alias'], $this->strLabel));