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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This condition will match when a field is valid (its validation returned no |
22
|
|
|
* error). |
23
|
|
|
*/ |
24
|
|
|
class FieldIsValidCondition extends AbstractConditionItem |
25
|
|
|
{ |
26
|
|
|
const CONDITION_NAME = 'fieldIsValid'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $javaScriptFiles = [ |
33
|
|
|
'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsValid.js' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
* @validate NotEmpty |
39
|
|
|
*/ |
40
|
|
|
protected $fieldName; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function getCssResult() |
46
|
|
|
{ |
47
|
|
|
return '[' . DataAttributesAssetHandler::getFieldDataValidKey($this->fieldName) . '="1"]'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function getJavaScriptResult() |
54
|
|
|
{ |
55
|
|
|
return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function getPhpResult(PhpConditionDataObject $dataObject) |
62
|
|
|
{ |
63
|
|
|
$field = $this->formObject |
64
|
|
|
->getConfiguration() |
65
|
|
|
->getField($this->fieldName); |
66
|
|
|
$result = $dataObject->getFormValidator() |
67
|
|
|
->validateField($field); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return false === $result->forProperty($this->fieldName)->hasErrors() |
70
|
|
|
&& false === $result->fieldIsDeactivated($field); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see validateConditionConfiguration() |
75
|
|
|
* @throws InvalidConditionException |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function checkConditionConfiguration() |
79
|
|
|
{ |
80
|
|
|
$configuration = $this->formObject->getConfiguration(); |
81
|
|
|
|
82
|
|
|
if (false === $configuration->hasField($this->fieldName)) { |
83
|
|
|
throw new InvalidConditionException( |
84
|
|
|
'The field "' . $this->fieldName . '" does not exist.', |
85
|
|
|
1488183577 |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getFieldName() |
96
|
|
|
{ |
97
|
|
|
return $this->fieldName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $fieldName |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setFieldName($fieldName) |
105
|
|
|
{ |
106
|
|
|
$this->fieldName = $fieldName; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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: