Skip to content

Commit 8412f63

Browse files
committed
Update HtmlFormField.php
1 parent 695c9a2 commit 8412f63

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed
+71-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ajax\semantic\html\collections\form;
43

54
use Ajax\JsUtils;
@@ -14,168 +13,180 @@
1413

1514
class HtmlFormField extends HtmlSemDoubleElement {
1615
use FieldTrait;
16+
1717
protected $_container;
18+
1819
protected $_validation;
19-
public function __construct($identifier, $field,$label=NULL) {
20-
parent::__construct($identifier, "div","field");
21-
$this->content=array();
22-
$this->_states=[State::ERROR,State::DISABLED];
23-
if(isset($label) && $label!=="")
20+
21+
public function __construct($identifier, $field, $label = NULL) {
22+
parent::__construct($identifier, "div", "field");
23+
$this->content = array();
24+
$this->_states = [
25+
State::ERROR,
26+
State::DISABLED
27+
];
28+
if (isset($label) && $label !== "")
2429
$this->setLabel($label);
2530
$this->setField($field);
26-
$this->_validation=NULL;
31+
$this->_validation = NULL;
2732
}
2833

29-
public function addPointingLabel($label,$pointing=Direction::NONE){
30-
$labelO=new HtmlLabel("",$label);
34+
public function addPointingLabel($label, $pointing = Direction::NONE) {
35+
$labelO = new HtmlLabel("", $label);
3136
$labelO->setPointing($pointing);
32-
$this->addContent($labelO,$pointing==="below" || $pointing==="right");
37+
$this->addContent($labelO, $pointing === "below" || $pointing === "right");
3338
return $labelO;
3439
}
3540

36-
public function setLabel($label){
37-
$labelO=$label;
38-
if(\is_string($label)){
39-
$labelO=new HtmlSemDoubleElement("","label","");
41+
public function setLabel($label) {
42+
$labelO = $label;
43+
if (\is_string($label)) {
44+
$labelO = new HtmlSemDoubleElement("", "label", "");
4045
$labelO->setContent($label);
41-
$labelO->setProperty("for", \str_replace("field-", "",$this->identifier));
46+
$labelO->setProperty("for", \str_replace("field-", "", $this->identifier));
4247
}
43-
$this->content["label"]=$labelO;
48+
$this->content["label"] = $labelO;
4449
}
4550

46-
public function setField($field){
47-
$this->content["field"]=$field;
51+
public function setField($field) {
52+
$this->content["field"] = $field;
4853
}
4954

5055
/**
5156
* Returns the label or null
57+
*
5258
* @return mixed
5359
*/
54-
public function getLabel(){
55-
if(\array_key_exists("label", $this->content))
60+
public function getLabel() {
61+
if (\array_key_exists("label", $this->content))
5662
return $this->content["label"];
5763
}
5864

5965
/**
6066
* Return the field
67+
*
6168
* @return mixed
6269
*/
63-
public function getField(){
70+
public function getField() {
6471
return $this->content["field"];
6572
}
6673

6774
/**
6875
* Return the field with data
76+
*
6977
* @return mixed
7078
*/
71-
public function getDataField(){
79+
public function getDataField() {
7280
return $this->content["field"];
7381
}
7482

7583
/**
7684
* puts the label before or behind
7785
*/
78-
public function swapLabel(){
79-
$label=$this->getLabel();
86+
public function swapLabel() {
87+
$label = $this->getLabel();
8088
unset($this->content["label"]);
81-
$this->content["label"]=$label;
89+
$this->content["label"] = $label;
8290
}
8391

8492
/**
8593
* Defines the field width
94+
*
8695
* @param int $width
8796
* @return \Ajax\semantic\html\collections\form\HtmlFormField
8897
*/
89-
public function setWidth($width){
90-
if(\is_int($width)){
91-
$width=Wide::getConstants()["W".$width];
98+
public function setWidth($width) {
99+
if (\is_int($width)) {
100+
$width = Wide::getConstants()["W" . $width];
92101
}
93102
$this->addToPropertyCtrl("class", $width, Wide::getConstants());
94-
if(isset($this->_container)){
103+
if (isset($this->_container)) {
95104
$this->_container->setEqualWidth(false);
96105
}
97-
return $this->addToPropertyCtrl("class", "wide",array("wide"));
106+
return $this->addToPropertyCtrl("class", "wide", array(
107+
"wide"
108+
));
98109
}
99110

100111
/**
101112
* Field displays an error state
113+
*
102114
* @return \Ajax\semantic\html\collections\form\HtmlFormField
103115
*/
104-
public function setError(){
116+
public function setError() {
105117
return $this->addToProperty("class", "error");
106118
}
107119

108-
public function setInline(){
120+
public function setInline() {
109121
return $this->addToProperty("class", "inline");
110122
}
111123

112-
public function jsState($state){
113-
return $this->jsDoJquery("addClass",$state);
124+
public function jsState($state) {
125+
return $this->jsDoJquery("addClass", $state);
114126
}
115127

116128
public function setContainer($_container) {
117-
$this->_container=$_container;
129+
$this->_container = $_container;
118130
return $this;
119131
}
120132

121-
public function setReadonly(){
133+
public function setReadonly() {
122134
$this->getDataField()->setProperty("readonly", "");
123135
}
124136

125-
public function addRule($type,$prompt=NULL,$value=NULL){
126-
$field=$this->getDataField();
127-
if(isset($field)){
128-
if(!isset($this->_validation)){
129-
$this->_validation=new FieldValidation($field->getIdentifier());
137+
public function addRule($type, $prompt = NULL, $value = NULL) {
138+
$field = $this->getDataField();
139+
if (isset($field)) {
140+
if (! isset($this->_validation)) {
141+
$this->_validation = new FieldValidation($field->getIdentifier());
130142
}
131-
if($type==="empty"){
132-
$this->addToProperty("class","required");
143+
if ($type === 'empty' || ($type['type'] ?? '') === 'empty') {
144+
$this->addToProperty('class', 'required');
133145
}
134-
$this->_validation->addRule($type,$prompt,$value);
146+
$this->_validation->addRule($type, $prompt, $value);
135147
}
136148
return $this;
137149
}
138-
139-
public function setOptional($optional=true){
140-
$field=$this->getDataField();
141-
if(isset($field)){
142-
if(!isset($this->_validation)){
143-
$this->_validation=new FieldValidation($field->getIdentifier());
150+
151+
public function setOptional($optional = true) {
152+
$field = $this->getDataField();
153+
if (isset($field)) {
154+
if (! isset($this->_validation)) {
155+
$this->_validation = new FieldValidation($field->getIdentifier());
144156
}
145157
$this->_validation->setOptional($optional);
146158
}
147159
}
148160

149-
public function addRules(array $rules){
150-
foreach ($rules as $rule){
161+
public function addRules(array $rules) {
162+
foreach ($rules as $rule) {
151163
$this->addRule($rule);
152164
}
153165
return $this;
154166
}
155167

156-
public function setRules(array $rules){
157-
$this->_validation=null;
168+
public function setRules(array $rules) {
169+
$this->_validation = null;
158170
return $this->addRules($rules);
159171
}
160172

161-
public function addIcon($icon,$direction=Direction::LEFT){
162-
$field=$this->getField();
163-
return $field->addIcon($icon,$direction);
173+
public function addIcon($icon, $direction = Direction::LEFT) {
174+
$field = $this->getField();
175+
return $field->addIcon($icon, $direction);
164176
}
165177

166178
public function getValidation() {
167179
return $this->_validation;
168180
}
169-
181+
170182
public function setSize($size) {
171183
return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants());
172184
}
173185

174186
public function run(JsUtils $js) {
175-
if(isset($this->_validation)){
187+
if (isset($this->_validation)) {
176188
$this->_validation->compile($js);
177189
}
178190
return parent::run($js);
179191
}
180-
181192
}

0 commit comments

Comments
 (0)