Skip to content

Commit aa0ebb9

Browse files
committed
DataForm v2
1 parent 667888e commit aa0ebb9

File tree

4 files changed

+61
-66
lines changed

4 files changed

+61
-66
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,19 @@ public function submitOnClick($identifier,$url,$responseElement){
212212
return $this->submitOn("click", $identifier, $url, $responseElement);
213213
}
214214

215-
public function addSubmit($identifier,$value,$CssStyle=NULL,$url=NULL,$responseElement=NULL){
216-
$bt=$this->addButton($identifier, $value,$CssStyle);
215+
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
216+
$bt=$this->addButton($identifier, $value,$cssStyle);
217217
if(isset($url) && isset($responseElement))
218218
$this->submitOnClick($identifier, $url, $responseElement);
219219
return $bt;
220220
}
221221

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+
222228
/**
223229
* Callback on each valid field
224230
* @param string $jsCode

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Ajax\semantic\html\elements\HtmlLabel;
1111
use Ajax\semantic\html\modules\HtmlProgress;
1212
use Ajax\semantic\html\modules\HtmlRating;
13-
use Ajax\semantic\html\base\HtmlSemDoubleElement;
14-
1513
/**
1614
* @author jc
1715
* @property InstanceViewer $_instanceViewer
@@ -27,19 +25,20 @@ private function _getLabelField($caption,$icon=NULL){
2725
return $label;
2826
}
2927

30-
/**
31-
* @param HtmlSemDoubleElement $element
32-
* @param array $attributes
33-
*/
34-
protected function _applyAttributes($element,&$attributes,$index){
35-
if(isset($attributes["callback"])){
36-
$callback=$attributes["callback"];
37-
if(\is_callable($callback)){
38-
$callback($element,$this->_modelInstance,$index);
39-
unset($attributes["callback"]);
28+
protected function _addRules($element,$attributes){}
29+
30+
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
31+
$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback,$prefix){
32+
$name=$this->_instanceViewer->getCaption($index)."[]";
33+
if(isset($attributes["name"])===true){
34+
$name=$attributes["name"];
4035
}
41-
}
42-
$element->fromArray($attributes);
36+
$element=$elementCallback($this->_getFieldIdentifier($prefix),$value,$name);
37+
if(\is_array($attributes))
38+
$this->_applyAttributes($element, $attributes,$index);
39+
return $element;
40+
});
41+
return $this;
4342
}
4443

4544

@@ -75,61 +74,43 @@ public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
7574
return $this;
7675
}
7776

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

8384

8485
public function fieldAsRadio($index,$attributes=NULL){
85-
$this->setValueFunction($index,function($value)use ($index,$attributes){
86-
if(isset($attributes["name"])===false){
87-
$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
88-
}
89-
$radio=new HtmlRadio($this->_getFieldIdentifier("radio"),$attributes["name"],$value,$value);
90-
$this->_applyAttributes($radio, $attributes, $index);
91-
return $radio;
92-
});
93-
return $this;
86+
return $this->_fieldAs(function($id,$value,$name){
87+
$input= new HtmlRadio($id,$name,$value,$value);
88+
return $input;
89+
}, $index,$attributes,"radio");
9490
}
9591

9692
public function fieldAsInput($index,$attributes=NULL){
97-
$this->setValueFunction($index,function($value) use($index,$attributes){
98-
$input=new HtmlInput($this->_getFieldIdentifier("input"),"text",$value);
99-
if(isset($attributes["name"])===false){
100-
$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
101-
}
102-
$input->getField()->setProperty("name", $attributes["name"]);
103-
$this->_applyAttributes($input, $attributes, $index);
93+
return $this->_fieldAs(function($id,$value,$name){
94+
$input= new HtmlInput($id,"text",$value);
95+
$input->getField()->setProperty("name", $name);
10496
return $input;
105-
});
106-
return $this;
97+
}, $index,$attributes,"input");
10798
}
10899

109100
public function fieldAsCheckbox($index,$attributes=NULL){
110-
$this->setValueFunction($index,function($value) use($index,$attributes){
111-
$checkbox=new HtmlCheckbox($this->_getFieldIdentifier("ck"),"",$value);
112-
$checkbox->setChecked(JString::isBooleanTrue($value));
113-
if(isset($attributes["name"])===false){
114-
$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
115-
}
116-
$checkbox->getField()->setProperty("name", $attributes["name"]);
117-
$this->_applyAttributes($checkbox, $attributes, $index);
118-
return $checkbox;
119-
});
120-
return $this;
101+
return $this->_fieldAs(function($id,$value,$name){
102+
$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
103+
$input->setChecked(JString::isBooleanTrue($value));
104+
$input->getField()->setProperty("name", $name);
105+
return $input;
106+
}, $index,$attributes,"ck");
121107
}
122108

123109
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
124-
$this->setValueFunction($index,function($value) use($index,$elements,$multiple,$attributes){
125-
$dd=new HtmlDropdown($this->_getFieldIdentifier("dd"),$value,$elements);
126-
if(isset($attributes["name"])===false){
127-
$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
128-
}
129-
$dd->asSelect($attributes["name"],$multiple);
130-
$this->_applyAttributes($dd, $attributes, $index);
110+
return $this->_fieldAs(function($id,$value,$name) use($elements,$multiple){
111+
$dd=new HtmlDropdown($id,$value,$elements);
112+
$dd->asSelect($name,$multiple);
131113
return $dd;
132-
});
133-
return $this;
114+
}, $index,$attributes,"dd");
134115
}
135116
}

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ protected function _setToolbarPosition($table, $captions=NULL) {
8787
public function setValidationParams(array $_validationParams){
8888
return $this->getHtmlComponent()->setValidationParams($_validationParams);
8989
}
90+
91+
public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
92+
return $this->getHtmlComponent()->addSubmit($identifier, $value,$cssStyle,$url,$responseElement);
93+
}
9094
}

Ajax/semantic/widgets/dataform/FormFieldAsTrait.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ private function _getLabelField($caption,$icon=NULL){
2929
* @param array $attributes
3030
*/
3131
protected function _applyAttributes($element,&$attributes,$index){
32-
if(isset($attributes["rules"])){
33-
$rules=$attributes["rules"];
34-
if(\is_array($rules))
35-
$element->addRules($rules);
36-
else
37-
$element->addRule($rules);
38-
unset($attributes["rules"]);
39-
}
32+
$this->_addRules($element, $attributes);
4033
if(isset($attributes["callback"])){
4134
$callback=$attributes["callback"];
4235
if(\is_callable($callback)){
@@ -47,7 +40,18 @@ protected function _applyAttributes($element,&$attributes,$index){
4740
$element->fromArray($attributes);
4841
}
4942

50-
protected function _fieldAs($elementCallback,$index,$attributes=NULL){
43+
protected function _addRules($element,$attributes){
44+
if(isset($attributes["rules"])){
45+
$rules=$attributes["rules"];
46+
if(\is_array($rules))
47+
$element->addRules($rules);
48+
else
49+
$element->addRule($rules);
50+
unset($attributes["rules"]);
51+
}
52+
}
53+
54+
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier=null){
5155
$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback){
5256
$caption=$this->_instanceViewer->getCaption($index);
5357
$name=$this->_instanceViewer->getFieldName($index);

0 commit comments

Comments
 (0)