Skip to content

Commit 030ef0e

Browse files
committed
FormTrait for refactoring
1 parent aa0ebb9 commit 030ef0e

File tree

6 files changed

+151
-87
lines changed

6 files changed

+151
-87
lines changed

Ajax/semantic/html/collections/form/HtmlForm.php

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Ajax\semantic\html\elements\HtmlDivider;
1111
use Ajax\JsUtils;
1212
use Ajax\service\AjaxCall;
13+
use Ajax\semantic\html\collections\form\traits\FormTrait;
1314

1415
/**
1516
* Semantic Form component
@@ -19,7 +20,7 @@
1920
*/
2021
class HtmlForm extends HtmlSemCollection {
2122

22-
use FieldsTrait;
23+
use FieldsTrait,FormTrait;
2324
/**
2425
* @var array
2526
*/
@@ -39,6 +40,10 @@ public function __construct($identifier, $elements=array()) {
3940
$this->addItems($elements);
4041
}
4142

43+
protected function getForm(){
44+
return $this;
45+
}
46+
4247
/**
4348
* @param string $title
4449
* @param number $niveau
@@ -182,67 +187,13 @@ public function run(JsUtils $js) {
182187
return $this->_bsComponent;
183188
}
184189

185-
public function setLoading() {
186-
return $this->addToProperty("class", "loading");
187-
}
188-
189-
public function addErrorMessage(){
190-
return $this->addContent((new HtmlMessage(""))->setError());
191-
}
192-
193-
public function jsState($state) {
194-
return $this->jsDoJquery("addClass", $state);
190+
public function addValidationParam($paramName,$paramValue){
191+
$this->_validationParams[$paramName]=$paramValue;
192+
return $this;
195193
}
196194

197195
public function setValidationParams(array $_validationParams) {
198196
$this->_validationParams=$_validationParams;
199197
return $this;
200198
}
201-
202-
public function submitOn($event,$identifier,$url,$responseElement){
203-
$elem=$this->getElementById($identifier, $this->content);
204-
if(isset($elem)){
205-
$elem->addEvent($event, "$('#".$this->identifier."').form('validate form');");
206-
$this->_validationParams["_ajaxSubmit"]=new AjaxCall("postForm", ["form"=>$this->identifier,"responseElement"=>$responseElement,"url"=>$url]);
207-
}
208-
return $this;
209-
}
210-
211-
public function submitOnClick($identifier,$url,$responseElement){
212-
return $this->submitOn("click", $identifier, $url, $responseElement);
213-
}
214-
215-
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
216-
$bt=$this->addButton($identifier, $value,$cssStyle);
217-
if(isset($url) && isset($responseElement))
218-
$this->submitOnClick($identifier, $url, $responseElement);
219-
return $bt;
220-
}
221-
222-
public function addReset($identifier,$value,$cssStyle=NULL){
223-
$bt=$this->addButton($identifier, $value,$cssStyle);
224-
$bt->setProperty("type", "reset");
225-
return $bt;
226-
}
227-
228-
/**
229-
* Callback on each valid field
230-
* @param string $jsCode
231-
* @return \Ajax\semantic\html\collections\form\HtmlForm
232-
*/
233-
public function onValid($jsCode){
234-
$this->_validationParams["onValid"]="%function(){".$jsCode."}%";
235-
return $this;
236-
}
237-
238-
/**
239-
* Callback if a form is all valid
240-
* @param string $jsCode can use event and fields parameters
241-
* @return HtmlForm
242-
*/
243-
public function onSuccess($jsCode){
244-
$this->_validationParams["onSuccess"]="%function(evt,fields){".$jsCode."}%";
245-
return $this;
246-
}
247-
248199
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ private function _getLabelField($caption,$icon=NULL){
2828
protected function _addRules($element,$attributes){}
2929

3030
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
31-
$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback,$prefix){
31+
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
3232
$name=$this->_instanceViewer->getCaption($index)."[]";
3333
if(isset($attributes["name"])===true){
3434
$name=$attributes["name"];
3535
}
36-
$element=$elementCallback($this->_getFieldIdentifier($prefix),$value,$name);
36+
$element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,"");
3737
if(\is_array($attributes))
3838
$this->_applyAttributes($element, $attributes,$index);
3939
return $element;
@@ -75,30 +75,31 @@ public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
7575
}
7676

7777
public function fieldAsAvatar($index,$attributes=NULL){
78-
return $this->_fieldAs(function($id,$value,$name){
79-
$img= (new HtmlImage($id,$value))->asAvatar();
78+
return $this->_fieldAs(function($id,$name,$value){
79+
$img=new HtmlImage($id,$value);
80+
$img->asAvatar();
8081
return $img;
8182
}, $index,$attributes,"avatar");
8283
}
8384

8485

8586
public function fieldAsRadio($index,$attributes=NULL){
86-
return $this->_fieldAs(function($id,$value,$name){
87+
return $this->_fieldAs(function($id,$name,$value){
8788
$input= new HtmlRadio($id,$name,$value,$value);
8889
return $input;
8990
}, $index,$attributes,"radio");
9091
}
9192

9293
public function fieldAsInput($index,$attributes=NULL){
93-
return $this->_fieldAs(function($id,$value,$name){
94+
return $this->_fieldAs(function($id,$name,$value){
9495
$input= new HtmlInput($id,"text",$value);
9596
$input->getField()->setProperty("name", $name);
9697
return $input;
9798
}, $index,$attributes,"input");
9899
}
99100

100101
public function fieldAsCheckbox($index,$attributes=NULL){
101-
return $this->_fieldAs(function($id,$value,$name){
102+
return $this->_fieldAs(function($id,$name,$value){
102103
$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
103104
$input->setChecked(JString::isBooleanTrue($value));
104105
$input->getField()->setProperty("name", $name);
@@ -107,7 +108,7 @@ public function fieldAsCheckbox($index,$attributes=NULL){
107108
}
108109

109110
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
110-
return $this->_fieldAs(function($id,$value,$name) use($elements,$multiple){
111+
return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
111112
$dd=new HtmlDropdown($id,$value,$elements);
112113
$dd->asSelect($name,$multiple);
113114
return $dd;

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ protected function _getValue($property,$index){
6767
elseif(\is_array($property)){
6868
$values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property);
6969
$value=\implode("", $values);
70-
}else
71-
$value=$property;
70+
}else{
71+
if(isset($this->values[$index])){
72+
$value= $this->values[$index]($property,$this->instance,$index);
73+
}else{
74+
$value=$property;
75+
}
76+
}
7277
}
7378
if(isset($this->afterCompile[$index])){
7479
if(\is_callable($this->afterCompile[$index])){

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Ajax\semantic\widgets\datatable\PositionInTable;
88
use Ajax\service\JArray;
99
use Ajax\JsUtils;
10+
use Ajax\semantic\html\collections\form\traits\FormTrait;
11+
use Ajax\semantic\html\elements\HtmlButton;
1012

1113
/**
1214
* DataForm widget for editing model objects
@@ -15,7 +17,7 @@
1517
* @since 2.2
1618
*/
1719
class DataForm extends Widget {
18-
use FormFieldAsTrait;
20+
use FormFieldAsTrait,FormTrait;
1921

2022
public function __construct($identifier, $modelInstance=NULL) {
2123
parent::__construct($identifier, null,$modelInstance);
@@ -54,6 +56,13 @@ protected function _generateContent($form){
5456
}
5557
}
5658

59+
/**
60+
* @return HtmlForm
61+
*/
62+
protected function getForm(){
63+
return $this->content["form"];
64+
}
65+
5766
public function addSeparatorAfter($fieldNum){
5867
$this->_instanceViewer->addSeparatorAfter($fieldNum);
5968
return $this;
@@ -68,6 +77,20 @@ public function setSeparators($separators) {
6877
return $this;
6978
}
7079

80+
public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
81+
$button=new HtmlButton($identifier,$value,$cssStyle);
82+
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
83+
return $this->addInToolbar($button);
84+
}
85+
86+
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
87+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){
88+
$button=new HtmlButton($id,$value,$cssStyle);
89+
$this->_buttonAsSubmit($button,"click",$url,$responseElement);
90+
return $button;
91+
}, $index,$attributes);
92+
}
93+
7194
/**
7295
* {@inheritDoc}
7396
* @see \Ajax\common\Widget::getHtmlComponent()
@@ -84,11 +107,8 @@ protected function _setToolbarPosition($table, $captions=NULL) {
84107
$this->content[$this->_toolbarPosition]=$this->_toolbar;
85108
}
86109

87-
public function setValidationParams(array $_validationParams){
88-
return $this->getHtmlComponent()->setValidationParams($_validationParams);
89-
}
90-
91-
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
92-
return $this->getHtmlComponent()->addSubmit($identifier, $value,$cssStyle,$url,$responseElement);
110+
public function setValidationParams(array $_validationParams) {
111+
$this->getForm()->setValidationParams($_validationParams);
112+
return $this;
93113
}
94114
}

Ajax/semantic/widgets/dataform/FormFieldAsTrait.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ protected function _addRules($element,$attributes){
5252
}
5353

5454
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier=null){
55-
$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback){
55+
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback){
5656
$caption=$this->_instanceViewer->getCaption($index);
5757
$name=$this->_instanceViewer->getFieldName($index);
58-
$element=$elementCallback($name,$caption,$value);
58+
$element=$elementCallback($name,$name,$value,$caption);
5959
if(\is_array($attributes))
6060
$this->_applyAttributes($element, $attributes,$index);
6161
return $element;
@@ -65,32 +65,32 @@ protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier
6565

6666

6767
public function fieldAsRadio($index,$attributes=NULL){
68-
return $this->_fieldAs(function($name,$caption,$value){
69-
return new HtmlFormRadio($name,$name,$caption,$value);
68+
return $this->_fieldAs(function($id,$name,$value,$caption){
69+
return new HtmlFormRadio($id,$name,$caption,$value);
7070
}, $index,$attributes);
7171
}
7272

7373
public function fieldAsTextarea($index,$attributes=NULL){
74-
return $this->_fieldAs(function($name,$caption,$value){
75-
return new HtmlFormTextarea($name,$caption,$value);
74+
return $this->_fieldAs(function($id,$name,$value,$caption){
75+
return new HtmlFormTextarea($id,$caption,$value);
7676
}, $index,$attributes);
7777
}
7878

7979
public function fieldAsInput($index,$attributes=NULL){
80-
return $this->_fieldAs(function($name,$caption,$value){
81-
return new HtmlFormInput($name,$caption,"text",$value);
80+
return $this->_fieldAs(function($id,$name,$value,$caption){
81+
return new HtmlFormInput($id,$caption,"text",$value);
8282
}, $index,$attributes);
8383
}
8484

8585
public function fieldAsCheckbox($index,$attributes=NULL){
86-
return $this->_fieldAs(function($name,$caption,$value){
87-
return new HtmlFormCheckbox($name,$caption,$value);
86+
return $this->_fieldAs(function($id,$name,$value,$caption){
87+
return new HtmlFormCheckbox($id,$caption,$value);
8888
}, $index,$attributes);
8989
}
9090

9191
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
92-
return $this->_fieldAs(function($name,$caption,$value) use ($elements,$multiple){
93-
return new HtmlFormDropdown($name,$elements,$caption,$value,$multiple);
92+
return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements,$multiple){
93+
return new HtmlFormDropdown($id,$elements,$caption,$value,$multiple);
9494
}, $index,$attributes);
9595
}
9696
}

0 commit comments

Comments
 (0)