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\AssetHandler\JavaScript; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\AbstractAssetHandler; |
17
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This asset handler generates the JavaScript code used the activate specific |
21
|
|
|
* validation rules for a given field. For example, you may want to enable the |
22
|
|
|
* rule "required" of a given field only under certain circumstances. |
23
|
|
|
*/ |
24
|
|
|
class FieldsValidationActivationJavaScriptAssetHandler extends AbstractAssetHandler |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Main function of this asset handler. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getFieldsValidationActivationJavaScriptCode() |
33
|
|
|
{ |
34
|
|
|
$javaScriptBlocks = []; |
35
|
|
|
$formConfiguration = $this->getFormObject()->getConfiguration(); |
36
|
|
|
|
37
|
|
|
foreach ($formConfiguration->getFields() as $fieldName => $field) { |
38
|
|
|
foreach ($field->getValidation() as $validatorName => $validation) { |
39
|
|
|
$fieldConditionExpression = []; |
40
|
|
|
$javaScriptTree = $this->conditionProcessor |
41
|
|
|
->getActivationConditionTreeForValidation($validation) |
42
|
|
|
->getJavaScriptConditions(); |
43
|
|
|
|
44
|
|
|
if (false === empty($javaScriptTree)) { |
45
|
|
|
foreach ($javaScriptTree as $node) { |
46
|
|
|
$fieldConditionExpression[] = 'flag = flag || (' . $node . ');'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$javaScriptBlocks[] = $this->getSingleFieldActivationConditionFunction($fieldName, $validatorName, $fieldConditionExpression); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$javaScriptBlocks = implode(CRLF, $javaScriptBlocks); |
55
|
|
|
$formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName()); |
56
|
|
|
|
57
|
|
|
return <<<JS |
58
|
|
|
(function() { |
59
|
|
|
Formz.Form.get( |
60
|
|
|
$formName, |
61
|
|
|
function(form) { |
62
|
|
|
var field = null; |
63
|
|
|
|
64
|
|
|
$javaScriptBlocks |
65
|
|
|
|
66
|
|
|
form.refreshAllFields(); |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
})(); |
70
|
|
|
JS; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This function is just here to make the class more readable. |
75
|
|
|
* |
76
|
|
|
* @param string $fieldName Name of the field. |
77
|
|
|
* @param string $validatorName Name of the validation rule. |
78
|
|
|
* @param array $fieldConditionExpression Array containing the JavaScript condition expression for the field. |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function getSingleFieldActivationConditionFunction($fieldName, $validatorName, $fieldConditionExpression) |
82
|
|
|
{ |
83
|
|
|
$fieldName = GeneralUtility::quoteJSvalue($fieldName); |
84
|
|
|
$validatorName = GeneralUtility::quoteJSvalue($validatorName); |
85
|
|
|
$fieldConditionExpression = implode(CRLF . str_repeat(' ', 20), $fieldConditionExpression); |
86
|
|
|
|
87
|
|
|
return <<<JS |
88
|
|
|
field = form.getFieldByName($fieldName); |
89
|
|
|
|
90
|
|
|
if (null !== field) { |
91
|
|
|
field.addActivationConditionForValidator( |
92
|
|
|
'__auto', |
93
|
|
|
$validatorName, |
94
|
|
|
function (field, continueValidation) { |
95
|
|
|
var flag = false; |
96
|
|
|
$fieldConditionExpression |
97
|
|
|
continueValidation(flag); |
98
|
|
|
} |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
JS; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|