phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
FormTrait.php
Go to the documentation of this file.
1<?php
3
12
19
23 abstract protected function getForm();
24
25
26 protected function addCompoValidation(Form $compo,HtmlFormField $field){
27 $validation=$field->getValidation();
28 if(isset($validation)){
29 $validation->setIdentifier($field->getDataField()->getIdentifier());
30 $compo->addFieldValidation($validation);
31 }
32 }
33
34 protected function addExtraCompoValidation(Form $compo,FieldValidation $validation){
35 $compo->addFieldValidation($validation);
36 }
37
38 protected function _runValidationParams(Form &$compo,JsUtils $js=NULL){
39 $form=$this->getForm();
40 $params=$form->getValidationParams();
41 if(isset($params["_ajaxSubmit"])){
42 $compilation=$this->_compileAjaxSubmit($params["_ajaxSubmit"],$js);
43 $this->onSuccess($compilation);
44 $form->removeValidationParam("_ajaxSubmit");
45 }
46 $compo->addParams($form->getValidationParams());
47 $form->setBsComponent($compo);
48 $form->addEventsOnRun($js);
49 }
50
51 protected function _compileAjaxSubmit($ajaxSubmit,JsUtils $js=null){
52 $compilation="";
53 if(\is_array($ajaxSubmit)){
54 foreach ($ajaxSubmit as $ajaxSubmitItem){
55 $compilation.=$ajaxSubmitItem->compile($js);
56 }
57 }elseif($ajaxSubmit instanceof AjaxCall){
58 $compilation=$ajaxSubmit->compile($js);
59 }
60 $compilation=str_ireplace("\"","%quote%", $compilation);
61 return $compilation;
62 }
63
64 public function setLoading() {
65 return $this->getForm()->addToProperty("class", "loading");
66 }
67
68 public function setState($state) {
69 $this->getForm()->addToProperty("class", $state);
70 return $this;
71 }
72
73 public function setAttached($value=true){
74 $form=$this->getForm();
75 if($value)
76 $form->addToPropertyCtrl("class", "attached", array ("attached" ));
77 return $form;
78 }
79
80 public function addErrorMessage(){
81 return $this->getForm()->addContent((new HtmlMessage(""))->setError());
82 }
83
84 public function jsState($state) {
85 return $this->getForm()->jsDoJquery("addClass", $state);
86 }
87
96 public function submitOn($event,$identifierOrElement,$url,$responseElement,$parameters=NULL){
97 $form=$this->getForm();
98 if($identifierOrElement instanceof BaseHtml)
99 $elem=$identifierOrElement;
100 else
101 $elem=$form->getElementById($identifierOrElement, $form->getContent());
102 if(isset($elem)){
103 $this->_buttonAsSubmit($elem, $event,$url,$responseElement,$parameters);
104 }
105 return $form;
106 }
107
108 public function submitOnClick($identifier,$url,$responseElement,$parameters=NULL){
109 return $this->submitOn("click", $identifier, $url, $responseElement,$parameters);
110 }
111
112 public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
113 $bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
114 return $this->_buttonAsSubmit($bt, "click",$url,$responseElement,$parameters);
115 }
116
117 protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL){
118 $form=$this->getForm();
119 if(isset($url) && isset($responseElement)){
120 $button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');",true,true);
121 $this->setSubmitParams($url,$responseElement,$parameters);
122 }
123 return $button;
124 }
125
126 public function setSubmitParams($url,$responseElement=NULL,$parameters=NULL){
127 $form=$this->getForm();
128 $params=["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url,"stopPropagation"=>true];
129 if(\is_array($parameters)){
130 $params=\array_merge($params,$parameters);
131 }
132 $form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", $params));
133 return $this;
134 }
135
136 public function addReset($identifier,$value,$cssStyle=NULL){
137 $bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
138 $bt->setProperty("type", "reset");
139 return $bt;
140 }
141
147 public function onValid($jsCode){
148 $form=$this->getForm();
149 $form->addValidationParam("onValid", "%function(){".$jsCode."}%");
150 return $form;
151 }
152
158 public function onSuccess($jsCode){
159 $form=$this->getForm();
160 $form->addValidationParam("onSuccess", $jsCode,"%function(event,fields){","}%");
161 return $form;
162 }
163
164 public function addExtraFieldRules($fieldname,$rules){
165 $form=$this->getForm();
166 $fv=$form->getExtraFieldValidation($fieldname);
167 foreach ($rules as $rule){
168 $fv->addRule($rule);
169 }
170 }
171
172 public function addExtraFieldRule($fieldname,$type,$prompt=NULL,$value=NULL){
173 $form=$this->getForm();
174 $fv=$form->getExtraFieldValidation($fieldname);
175 $fv->addRule($type,$prompt,$value);
176 }
177
178 public function setOptional($fieldname,$optional=true){
179 $form=$this->getForm();
180 $fv=$form->getExtraFieldValidation($fieldname);
181 $fv->setOptional($optional);
182 }
183
184 public function setActionTarget(string $action, string $target){
185 $form=$this->getForm();
186 $form->setProperty('data-target',$target);
187 $form->setProperty('action',$action);
188 }
189}
JQuery PHP library.
Definition JsUtils.php:23
BaseHtml for HTML components.
Definition BaseHtml.php:17
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
addFieldValidation($fieldValidation)
Definition Form.php:53
submitOnClick($identifier, $url, $responseElement, $parameters=NULL)
_buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement=NULL, $parameters=NULL)
addReset($identifier, $value, $cssStyle=NULL)
addExtraFieldRule($fieldname, $type, $prompt=NULL, $value=NULL)
submitOn($event, $identifierOrElement, $url, $responseElement, $parameters=NULL)
Definition FormTrait.php:96
onValid($jsCode)
Callback on each valid field.
onSuccess($jsCode)
Callback if a form is all valid.
addExtraCompoValidation(Form $compo, FieldValidation $validation)
Definition FormTrait.php:34
setActionTarget(string $action, string $target)
_runValidationParams(Form &$compo, JsUtils $js=NULL)
Definition FormTrait.php:38
setSubmitParams($url, $responseElement=NULL, $parameters=NULL)
_compileAjaxSubmit($ajaxSubmit, JsUtils $js=null)
Definition FormTrait.php:51
addCompoValidation(Form $compo, HtmlFormField $field)
Definition FormTrait.php:26
addSubmit($identifier, $value, $cssStyle=NULL, $url=NULL, $responseElement=NULL, $parameters=NULL)