/
AddFormControlClientValidatorTag.php
118 lines (104 loc) · 3.78 KB
/
AddFormControlClientValidatorTag.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
<?php
/**
* <!--
* This file is part of the adventure php framework (APF) published under
* https://adventure-php-framework.org.
*
* The APF is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The APF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the APF. If not, see http://www.gnu.org/licenses/lgpl-3.0.txt.
* -->
*/
namespace APF\extensions\form\client\taglib;
use APF\extensions\form\client\ClientValidationScriptStore;
use APF\tools\form\FormException;
use APF\tools\form\taglib\AbstractFormControl;
use APF\tools\form\taglib\HtmlFormTag;
/**
* This taglib adds validators to one or more controls.
*
* @author Ralf Schubert <ralf.schubert@the-screeze.de>
* @version
* Version 1.0, 18.03.2010<br />
*/
class AddFormControlClientValidatorTag extends AbstractFormControl {
// prevent missing name error
public function onParseTime() {
}
/**
* Adds a validator to one or more controls
*
* @author Ralf Schubert
* @version
* Version 1.0, 18.03.2010<br />
*/
public function transform() {
$class = $this->getAttribute('class');
$controlsTmp = explode('|', $this->getAttribute('control'));
$button = $this->getAttribute('button');
$onblur = false;
$options = null;
$controls = [];
if ($this->getAttribute('onblur') === 'true') {
$onblur = true;
}
// Configure referenced controls
/* @var $parent HtmlFormTag */
$parent = $this->getParent();
foreach ($controlsTmp as $control) {
if (($ref = $parent->getFormElementByName($control)->getAttribute('ref')) !== null) {
$controlsTmp[] = $ref;
try {
$refField = $parent->getFormElementByName($ref);
} catch (FormException $e) {
throw new FormException('[AddFormControlClientValidatorTag::transform()]
No form element with name "' . $ref . '" found!
Check attribute "ref" of element "' . $control . '"!');
}
$refField->setAttribute('ref', $control);
unset($refField);
}
}
$options = [];
foreach ($controlsTmp as $control) {
//Get valmarkerclass of each control
$valmarkerclass = 'apf-form-error';
if (($val = $parent->getFormElementByName($control)->getAttribute('valmarkerclass')) !== null) {
$valmarkerclass = $val;
unset($val);
}
$controls[$control] = ['control' => $control, 'valmarkerclass' => $valmarkerclass];
unset($valmarkerclass);
//Get parameters of each control
$rawOptions = $parent->getFormElementByName($control)->getAttributes();
unset($rawOptions['name']);
unset($rawOptions['valmarkerclass']);
$options[$control] = $rawOptions;
}
/* @var $CVSS ClientValidationScriptStore */
$CVSS = $this->getServiceObject(ClientValidationScriptStore::class);
$CVSS->addClientValidator($class, $button, $controls, $options, $onblur);
}
/**
* Overwrite the parent's method, because there's nothing to do here.
*
* @author Ralf Schubert
* @version
* Version 1.0, 18.03.2010<br />
*/
public function onAfterAppend() {
}
public function reset() {
// nothing to do as client listeners cannot be reset
return $this;
}
}