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
|
|
|
|
8
|
|
|
trait FormTrait{ |
9
|
|
|
/** |
10
|
|
|
* @return HtmlForm |
11
|
|
|
*/ |
12
|
|
|
abstract protected function getForm(); |
13
|
|
|
|
14
|
|
|
public function setLoading() { |
15
|
|
|
return $this->getForm()->addToProperty("class", "loading"); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function addErrorMessage(){ |
19
|
|
|
return $this->getForm()->addContent((new HtmlMessage(""))->setError()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function jsState($state) { |
23
|
|
|
return $this->getForm()->jsDoJquery("addClass", $state); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $event |
28
|
|
|
* @param string $identifier |
29
|
|
|
* @param string $url |
30
|
|
|
* @param string $responseElement |
31
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
32
|
|
|
*/ |
33
|
|
|
public function submitOn($event,$identifier,$url,$responseElement){ |
34
|
|
|
$form=$this->getForm(); |
35
|
|
|
$elem=$form->getElementById($identifier, $form->getContent()); |
36
|
|
|
if(isset($elem)){ |
37
|
|
|
$this->_buttonAsSubmit($elem, $event,$url,$responseElement); |
38
|
|
|
} |
39
|
|
|
return $form; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function submitOnClick($identifier,$url,$responseElement){ |
43
|
|
|
return $this->submitOn("click", $identifier, $url, $responseElement); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){ |
47
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
48
|
|
|
return $this->_buttonAsSubmit($bt, "click",$url,$responseElement); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function _buttonAsSubmit($button,$event,$url,$responseElement=NULL){ |
52
|
|
|
$form=$this->getForm(); |
53
|
|
|
if(isset($url) && isset($responseElement)){ |
54
|
|
|
$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');"); |
55
|
|
|
$form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", ["form"=>$this->identifier,"responseElement"=>$responseElement,"url"=>$url])); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
return $button; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addReset($identifier,$value,$cssStyle=NULL){ |
61
|
|
|
$bt=$this->getForm()->addButton($identifier, $value,$cssStyle); |
62
|
|
|
$bt->setProperty("type", "reset"); |
63
|
|
|
return $bt; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Callback on each valid field |
68
|
|
|
* @param string $jsCode |
69
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
70
|
|
|
*/ |
71
|
|
|
public function onValid($jsCode){ |
72
|
|
|
$form=$this->getForm(); |
73
|
|
|
$form->addValidationParam("onValid", "%function(){".$jsCode."}%"); |
74
|
|
|
return $form; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Callback if a form is all valid |
79
|
|
|
* @param string $jsCode can use event and fields parameters |
80
|
|
|
* @return HtmlForm |
81
|
|
|
*/ |
82
|
|
|
public function onSuccess($jsCode){ |
83
|
|
|
$form=$this->getForm(); |
84
|
|
|
$form->addValidationParam("onSuccess", "%function(evt,fields){".$jsCode."}%"); |
85
|
|
|
return $form; |
86
|
|
|
} |
87
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: