Skip to content

Commit 778966e

Browse files
committed
FieldAs, Form light refactoring
1 parent 686575d commit 778966e

File tree

8 files changed

+46
-23
lines changed

8 files changed

+46
-23
lines changed

Ajax/common/Widget.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ public function addField($field){
122122
return $this;
123123
}
124124

125+
public function addMessage($attributes=NULL,$fieldName="message"){
126+
$this->_instanceViewer->addField($fieldName);
127+
$count=$this->_instanceViewer->visiblePropertiesCount();
128+
return $this->fieldAsMessage($count-1,$attributes);
129+
}
130+
131+
public function addErrorMessage(){
132+
return $this->addMessage(["error"=>true],"message");
133+
}
134+
125135
public function insertField($index,$field){
126136
$this->_instanceViewer->insertField($index, $field);
127137
return $this;
@@ -331,15 +341,15 @@ public function run(JsUtils $js=NULL){
331341
return $result;
332342
}
333343

334-
protected function runForm(JsUtils $js){
344+
protected function runForm(JsUtils $js=NULL){
335345
$fields=$this->getContentInstances(HtmlFormField::class);
336346
foreach ($fields as $field){
337347
$this->_form->addField($field);
338348
}
339349
return $this->_form->run($js);
340350
}
341351

342-
protected function _compileForm(JsUtils $js=NULL,&$view=NULL){
352+
protected function _compileForm(){
343353
if(isset($this->_form)){
344354
$noValidate="";
345355
if(\sizeof($this->_form->getValidationParams())>0)

Ajax/common/html/html5/HtmlInput.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Ajax\common\html\HtmlSingleElement;
66
use Ajax\service\JString;
7+
use Ajax\JsUtils;
78

89
class HtmlInput extends HtmlSingleElement {
9-
10+
protected $_placeholder;
1011
public function __construct($identifier,$type="text",$value=NULL,$placeholder=NULL) {
1112
parent::__construct($identifier, "input");
1213
$this->setProperty("name", $identifier);
@@ -26,12 +27,18 @@ public function setInputType($value) {
2627
}
2728

2829
public function setPlaceholder($value){
29-
if(JString::isNull($value)){
30-
if(JString::isNotNull($this->identifier))
31-
$value=\ucfirst($this->identifier);
32-
}
3330
if(JString::isNotNull($value))
34-
$this->setProperty("placeholder", $value);
31+
$this->_placeholder=$value;
3532
return $this;
3633
}
34+
35+
public function compile(JsUtils $js=NULL,&$view=NULL){
36+
$value=$this->_placeholder;
37+
if(JString::isNull($value)){
38+
if(JString::isNotNull($this->getProperty("name")))
39+
$value=\ucfirst($this->getProperty("name"));
40+
}
41+
$this->setProperty("placeholder", $value);
42+
return parent::compile($js,$view);
43+
}
3744
}

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

Lines changed: 9 additions & 6 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\semantic\html\collections\form\traits\FormTrait;
13+
use Ajax\semantic\components\Form;
1314

1415
/**
1516
* Semantic Form component
@@ -151,24 +152,26 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
151152
}
152153

153154
public function run(JsUtils $js) {
154-
$compo=NULL;
155+
if(isset($js)){
156+
$compo=$js->semantic()->form("#".$this->identifier);
157+
}else{
158+
$compo=new Form();
159+
$compo->attach("#".$this->identifier);
160+
}
155161
foreach ($this->_fields as $field){
156162
if($field instanceof HtmlFormField){
157-
$compo=$this->addCompoValidation($js, $compo, $field);
163+
$compo=$this->addCompoValidation($compo, $field);
158164
}
159165
}
160166
foreach ($this->content as $field){
161167
if($field instanceof HtmlFormFields){
162168
$items=$field->getItems();
163169
foreach ($items as $_field){
164170
if($_field instanceof HtmlFormField)
165-
$compo=$this->addCompoValidation($js, $compo, $_field);
171+
$compo=$this->addCompoValidation($compo, $_field);
166172
}
167173
}
168174
}
169-
if(isset($compo)===false){
170-
return parent::run($js);
171-
}
172175
$this->_runValidationParams($compo,$js);
173176
return $this->_bsComponent;
174177
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public function addRules(array $rules){
141141
return $this;
142142
}
143143

144+
public function setRules(array $rules){
145+
$this->_validation=null;
146+
return $this->addRules($rules);
147+
}
148+
144149
public function addIcon($icon,$direction=Direction::LEFT){
145150
$field=$this->getField();
146151
return $field->addIcon($icon,$direction);

Ajax/semantic/html/collections/form/traits/FormTrait.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ trait FormTrait{
1313
*/
1414
abstract protected function getForm();
1515

16-
protected function addCompoValidation($js,$compo,$field){
17-
$form=$this->getForm();
16+
protected function addCompoValidation($compo,$field){
1817
$validation=$field->getValidation();
1918
if(isset($validation)){
20-
if(isset($compo)===false){
21-
$compo=$js->semantic()->form("#".$form->getIdentifier());
22-
}
2319
$validation->setIdentifier($field->getDataField()->getIdentifier());
2420
$compo->addFieldValidation($validation);
2521
}

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ public function fieldsAs(array $types){
209209
$i=0;
210210
if(JArray::isAssociative($types)){
211211
foreach ($types as $type=>$attributes){
212-
$this->fieldAs($i++,$type,$attributes);
212+
if(\is_int($type))
213+
$this->fieldAs($i++,$attributes,[]);
214+
else
215+
$this->fieldAs($i++,$type,$attributes);
213216
}
214217
}else{
215218
foreach ($types as $type){
@@ -220,7 +223,6 @@ public function fieldsAs(array $types){
220223

221224
public function fieldAs($index,$type,$attributes=NULL){
222225
$method="fieldAs".\ucfirst($type);
223-
224226
if(\method_exists($this, $method)){
225227
if(!\is_array($attributes)){
226228
$attributes=[$index];

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
3434
$this->_setToolbarPosition($table);
3535
}
3636
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
37-
$this->_compileForm($js,$view);
37+
$this->_compileForm();
3838
return parent::compile($js,$view);
3939
}
4040

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
8585
$this->_setToolbarPosition($table, $captions);
8686
}
8787
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
88-
$this->_compileForm($js,$view);
88+
$this->_compileForm();
8989
return parent::compile($js,$view);
9090
}
9191

0 commit comments

Comments
 (0)