1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\html\collections\form\traits; |
3
|
|
|
|
4
|
|
|
use Ajax\semantic\html\collections\form\HtmlForm; |
5
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
6
|
|
|
use Ajax\service\AjaxCall; |
7
|
|
|
use Ajax\JsUtils; |
8
|
|
|
use Ajax\common\html\BaseHtml; |
9
|
|
|
use Ajax\semantic\components\Form; |
10
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormField; |
11
|
|
|
use Ajax\semantic\components\validation\FieldValidation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* trait used in Widget and HtmlForm |
15
|
|
|
* @author jc |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
trait FormTrait{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return HtmlForm |
22
|
|
|
*/ |
23
|
|
|
abstract protected function getForm(); |
24
|
|
|
|
25
|
|
|
protected function addCompoValidation(Form $compo,HtmlFormField $field){ |
26
|
|
|
$validation=$field->getValidation(); |
27
|
|
|
if(isset($validation)){ |
28
|
|
|
$validation->setIdentifier($field->getDataField()->getIdentifier()); |
29
|
|
|
$compo->addFieldValidation($validation); |
30
|
|
|
} |
31
|
|
|
return $compo; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function addExtraCompoValidation(Form $compo,FieldValidation $validation){ |
35
|
|
|
$compo->addFieldValidation($validation); |
36
|
|
|
return $compo; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function _runValidationParams(Form &$compo,JsUtils $js=NULL){ |
40
|
|
|
$form=$this->getForm(); |
41
|
|
|
$params=$form->getValidationParams(); |
42
|
|
|
if(isset($params["_ajaxSubmit"])){ |
43
|
|
|
$compilation=$this->_compileAjaxSubmit($params["_ajaxSubmit"],$js); |
44
|
|
|
$this->onSuccess($compilation); |
45
|
|
|
$form->removeValidationParam("_ajaxSubmit"); |
46
|
|
|
} |
47
|
|
|
$compo->addParams($form->getValidationParams()); |
48
|
|
|
$form->setBsComponent($compo); |
49
|
|
|
$form->addEventsOnRun($js); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function _compileAjaxSubmit($ajaxSubmit,JsUtils $js=null){ |
53
|
|
|
$compilation=""; |
54
|
|
|
if(\is_array($ajaxSubmit)){ |
55
|
|
|
foreach ($ajaxSubmit as $ajaxSubmitItem){ |
56
|
|
|
$compilation.=$ajaxSubmitItem->compile($js); |
57
|
|
|
} |
58
|
|
|
}elseif($ajaxSubmit instanceof AjaxCall){ |
59
|
|
|
$compilation=$ajaxSubmit->compile($js); |
60
|
|
|
} |
61
|
|
|
$compilation=str_ireplace("\"","%quote%", $compilation); |
62
|
|
|
return $compilation; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setLoading() { |
66
|
|
|
return $this->getForm()->addToProperty("class", "loading"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setState($state) { |
70
|
|
|
$this->getForm()->addToProperty("class", $state); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setAttached($value=true){ |
75
|
|
|
$form=$this->getForm(); |
76
|
|
|
if($value) |
77
|
|
|
$form->addToPropertyCtrl("class", "attached", array ("attached" )); |
78
|
|
|
return $form; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function addErrorMessage(){ |
82
|
|
|
return $this->getForm()->addContent((new HtmlMessage(""))->setError()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function jsState($state) { |
86
|
|
|
return $this->getForm()->jsDoJquery("addClass", $state); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $event |
91
|
|
|
* @param string|BaseHtml $identifierOrElement |
92
|
|
|
* @param string $url |
93
|
|
|
* @param string $responseElement |
94
|
|
|
* @param array $parameters |
95
|
|
|
* @return HtmlForm |
96
|
|
|
*/ |
97
|
|
|
public function submitOn($event,$identifierOrElement,$url,$responseElement,$parameters=NULL){ |
98
|
|
|
$form=$this->getForm(); |
99
|
|
|
if($identifierOrElement instanceof BaseHtml) |
100
|
|
|
$elem=$identifierOrElement; |
101
|
|
|
else |
102
|
|
|
$elem=$form->getElementById($identifierOrElement, $form->getContent()); |
103
|
|
|
if(isset($elem)){ |
104
|
|
|
$this->_buttonAsSubmit($elem, $event,$url,$responseElement,$parameters); |
105
|
|
|
} |
106
|
|
|
return $form; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function submitOnClick($identifier,$url,$responseElement,$parameters=NULL){ |
110
|
|
|
return $this->submitOn("click", $identifier, $url, $responseElement,$parameters); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){ |
114
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
115
|
|
|
return $this->_buttonAsSubmit($bt, "click",$url,$responseElement,$parameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL){ |
119
|
|
|
$form=$this->getForm(); |
120
|
|
|
if(isset($url) && isset($responseElement)){ |
121
|
|
|
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');",true,true); |
122
|
|
|
$this->setSubmitParams($url,$responseElement,$parameters); |
123
|
|
|
} |
124
|
|
|
return $button; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setSubmitParams($url,$responseElement=NULL,$parameters=NULL){ |
128
|
|
|
$form=$this->getForm(); |
129
|
|
|
$params=["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url,"stopPropagation"=>true]; |
130
|
|
|
if(\is_array($parameters)){ |
131
|
|
|
$params=\array_merge($params,$parameters); |
132
|
|
|
} |
133
|
|
|
$form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", $params)); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function addReset($identifier,$value,$cssStyle=NULL){ |
138
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
139
|
|
|
$bt->setProperty("type", "reset"); |
140
|
|
|
return $bt; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Callback on each valid field |
145
|
|
|
* @param string $jsCode |
146
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
147
|
|
|
*/ |
148
|
|
|
public function onValid($jsCode){ |
149
|
|
|
$form=$this->getForm(); |
150
|
|
|
$form->addValidationParam("onValid", "%function(){".$jsCode."}%"); |
151
|
|
|
return $form; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Callback if a form is all valid |
156
|
|
|
* @param string $jsCode can use event and fields parameters |
157
|
|
|
* @return HtmlForm |
158
|
|
|
*/ |
159
|
|
|
public function onSuccess($jsCode){ |
160
|
|
|
$form=$this->getForm(); |
161
|
|
|
$form->addValidationParam("onSuccess", $jsCode,"%function(event,fields){","}%"); |
162
|
|
|
return $form; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function addExtraFieldRules($fieldname,$rules){ |
166
|
|
|
$form=$this->getForm(); |
167
|
|
|
$fv=$form->getExtraFieldValidation($fieldname); |
168
|
|
|
foreach ($rules as $rule){ |
169
|
|
|
$fv->addRule($rule); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function addExtraFieldRule($fieldname,$type,$prompt=NULL,$value=NULL){ |
174
|
|
|
$form=$this->getForm(); |
175
|
|
|
$fv=$form->getExtraFieldValidation($fieldname); |
176
|
|
|
$fv->addRule($type,$prompt,$value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function setOptional($fieldname,$optional=true){ |
180
|
|
|
$form=$this->getForm(); |
181
|
|
|
$fv=$form->getExtraFieldValidation($fieldname); |
182
|
|
|
$fv->setOptional($optional); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|