[BUGFIX] EXT:form - Validation of radiogroup/ checkboxgroup elements
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Validator / RequiredValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Validator;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 class RequiredValidator extends AbstractValidator
18 {
19 /**
20 * Constant for localisation
21 *
22 * @var string
23 */
24 const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_required';
25
26 /**
27 * @var bool
28 */
29 protected $allFieldsAreEmpty = true;
30
31 /**
32 * Check if $value is valid. If it is not valid, needs to add an error
33 * to result.
34 *
35 * @param mixed $value
36 * @return void
37 */
38 public function isValid($value)
39 {
40 if (is_array($value)) {
41 array_walk_recursive($value, function ($value, $key, $validator) {
42 if (!empty($value) || $value === '0' || $value === 0) {
43 $validator->setAllFieldsAreEmpty(false);
44 }
45 },
46 $this
47 );
48 if ($this->getAllFieldsAreEmpty()) {
49 $this->addError(
50 $this->renderMessage(
51 $this->options['errorMessage'][0],
52 $this->options['errorMessage'][1],
53 'error'
54 ),
55 1441980673
56 );
57 }
58 } else {
59 if (
60 empty($value)
61 && $value !== 0
62 && $value !== '0'
63 ) {
64 $this->addError(
65 $this->renderMessage(
66 $this->options['errorMessage'][0],
67 $this->options['errorMessage'][1],
68 'error'
69 ),
70 144198067
71 );
72 }
73 }
74 }
75
76 /**
77 * A helper method for the array_walk_recursive callback in the
78 * function isValid().
79 * If the callback detect a empty value, the
80 * property allFieldsAreEmpty is set to TRUE.
81 *
82 * @param bool $allFieldsAreEmpty
83 * @return void
84 */
85 protected function setAllFieldsAreEmpty($allFieldsAreEmpty = true)
86 {
87 $this->allFieldsAreEmpty = $allFieldsAreEmpty;
88 }
89
90 /**
91 * A helper method for the array_walk_recursive callback in the
92 * function isValid().
93 * If the callback detect a empty value, the
94 * property allFieldsAreEmpty is set to TRUE.
95 *
96 * @return bool
97 */
98 protected function getAllFieldsAreEmpty()
99 {
100 return $this->allFieldsAreEmpty;
101 }
102 }