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
|
|
|
|
9
|
|
|
trait FormTrait{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @return HtmlForm |
13
|
|
|
*/ |
14
|
|
|
abstract protected function getForm(); |
15
|
|
|
|
16
|
|
|
protected function addCompoValidation($js,$compo,$field){ |
17
|
|
|
$form=$this->getForm(); |
18
|
|
|
$validation=$field->getValidation(); |
19
|
|
|
if(isset($validation)){ |
20
|
|
|
if(isset($compo)===false){ |
21
|
|
|
$compo=$js->semantic()->form("#".$form->getIdentifier()); |
22
|
|
|
} |
23
|
|
|
$validation->setIdentifier($field->getDataField()->getIdentifier()); |
24
|
|
|
$compo->addFieldValidation($validation); |
25
|
|
|
} |
26
|
|
|
return $compo; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function _runValidationParams(&$compo,JsUtils $js=NULL){ |
30
|
|
|
$form=$this->getForm(); |
31
|
|
|
$params=$form->getValidationParams(); |
32
|
|
|
if(isset($params["_ajaxSubmit"]) && $params["_ajaxSubmit"] instanceof AjaxCall){ |
33
|
|
|
$compilation=$params["_ajaxSubmit"]->compile($js); |
34
|
|
|
$compilation=str_ireplace("\"","%quote%", $compilation); |
35
|
|
|
$this->onSuccess($compilation); |
36
|
|
|
unset($params["_ajaxSubmit"]); |
37
|
|
|
} |
38
|
|
|
$compo->addParams($params); |
39
|
|
|
$form->setBsComponent($compo); |
40
|
|
|
$form->addEventsOnRun($js); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setLoading() { |
44
|
|
|
return $this->getForm()->addToProperty("class", "loading"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function addErrorMessage(){ |
48
|
|
|
return $this->getForm()->addContent((new HtmlMessage(""))->setError()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function jsState($state) { |
52
|
|
|
return $this->getForm()->jsDoJquery("addClass", $state); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $event |
57
|
|
|
* @param string $identifier |
58
|
|
|
* @param string $url |
59
|
|
|
* @param string $responseElement |
60
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
61
|
|
|
*/ |
62
|
|
|
public function submitOn($event,$identifier,$url,$responseElement){ |
63
|
|
|
$form=$this->getForm(); |
64
|
|
|
$elem=$form->getElementById($identifier, $form->getContent()); |
65
|
|
|
if(isset($elem)){ |
66
|
|
|
$this->_buttonAsSubmit($elem, $event,$url,$responseElement); |
67
|
|
|
} |
68
|
|
|
return $form; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function submitOnClick($identifier,$url,$responseElement){ |
72
|
|
|
return $this->submitOn("click", $identifier, $url, $responseElement); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){ |
76
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
77
|
|
|
return $this->_buttonAsSubmit($bt, "click",$url,$responseElement); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL){ |
81
|
|
|
$form=$this->getForm(); |
82
|
|
|
if(isset($url) && isset($responseElement)){ |
83
|
|
|
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');"); |
84
|
|
|
$form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", ["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url])); |
85
|
|
|
} |
86
|
|
|
return $button; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function addReset($identifier,$value,$cssStyle=NULL){ |
90
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
91
|
|
|
$bt->setProperty("type", "reset"); |
92
|
|
|
return $bt; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Callback on each valid field |
97
|
|
|
* @param string $jsCode |
98
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
99
|
|
|
*/ |
100
|
|
|
public function onValid($jsCode){ |
101
|
|
|
$form=$this->getForm(); |
102
|
|
|
$form->addValidationParam("onValid", "%function(){".$jsCode."}%"); |
103
|
|
|
return $form; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Callback if a form is all valid |
108
|
|
|
* @param string $jsCode can use event and fields parameters |
109
|
|
|
* @return HtmlForm |
110
|
|
|
*/ |
111
|
|
|
public function onSuccess($jsCode){ |
112
|
|
|
$form=$this->getForm(); |
113
|
|
|
$form->addValidationParam("onSuccess", "%function(evt,fields){".$jsCode."}%"); |
114
|
|
|
return $form; |
115
|
|
|
} |
116
|
|
|
} |