1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Formz project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Condition\Items; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler; |
17
|
|
|
use Romm\Formz\Condition\Exceptions\InvalidConditionException; |
18
|
|
|
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject; |
19
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This condition will match when a field is does have a specific error. |
23
|
|
|
* |
24
|
|
|
* Note: an error is identified by a name of validation (example "isValid"), and |
25
|
|
|
* by the name of the error returned by the validator ("default" by default). |
26
|
|
|
*/ |
27
|
|
|
class FieldHasErrorCondition extends AbstractConditionItem |
28
|
|
|
{ |
29
|
|
|
const CONDITION_NAME = 'fieldHasError'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected static $javaScriptFiles = [ |
36
|
|
|
'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasError.js' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
* @validate NotEmpty |
42
|
|
|
*/ |
43
|
|
|
protected $fieldName; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* @validate NotEmpty |
48
|
|
|
*/ |
49
|
|
|
protected $validationName; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
* @validate NotEmpty |
54
|
|
|
*/ |
55
|
|
|
protected $errorName = 'default'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function getCssResult() |
61
|
|
|
{ |
62
|
|
|
return sprintf( |
63
|
|
|
'[%s="1"]', |
64
|
|
|
DataAttributesAssetHandler::getFieldDataValidationErrorKey($this->fieldName, $this->validationName, $this->errorName) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function getJavaScriptResult() |
72
|
|
|
{ |
73
|
|
|
return $this->getDefaultJavaScriptCall( |
74
|
|
|
[ |
75
|
|
|
'fieldName' => $this->fieldName, |
76
|
|
|
'validationName' => $this->validationName, |
77
|
|
|
'errorName' => $this->errorName |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function getPhpResult(PhpConditionDataObject $dataObject) |
86
|
|
|
{ |
87
|
|
|
$flag = false; |
88
|
|
|
$field = $this->formObject |
89
|
|
|
->getConfiguration() |
90
|
|
|
->getField($this->fieldName); |
91
|
|
|
$result = $dataObject->getFormValidator() |
92
|
|
|
->validateField($field) |
|
|
|
|
93
|
|
|
->forProperty($this->fieldName); |
94
|
|
|
|
95
|
|
|
foreach ($result->getErrors() as $error) { |
96
|
|
|
if ($error instanceof FormzMessageInterface |
97
|
|
|
&& $this->validationName === $error->getValidationName() |
98
|
|
|
&& $this->errorName === $error->getMessageKey() |
99
|
|
|
) { |
100
|
|
|
$flag = true; |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $flag; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getFieldName() |
112
|
|
|
{ |
113
|
|
|
return $this->fieldName; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getValidationName() |
120
|
|
|
{ |
121
|
|
|
return $this->validationName; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getErrorName() |
128
|
|
|
{ |
129
|
|
|
return $this->errorName; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @see validateConditionConfiguration() |
134
|
|
|
* @throws InvalidConditionException |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected function checkConditionConfiguration() |
138
|
|
|
{ |
139
|
|
|
$configuration = $this->formObject->getConfiguration(); |
140
|
|
|
|
141
|
|
|
if (false === $configuration->hasField($this->fieldName)) { |
142
|
|
|
throw new InvalidConditionException( |
143
|
|
|
'The field "' . $this->fieldName . '" does not exist.', |
144
|
|
|
1488192037 |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (false === $configuration->getField($this->fieldName)->hasValidation($this->validationName)) { |
149
|
|
|
throw new InvalidConditionException( |
150
|
|
|
'The validation "' . $this->validationName . '" does not exist for the field "' . $this->fieldName . '".', |
151
|
|
|
1488192055 |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: