|
1 | 1 | <?php
|
2 | | - |
3 | 2 | namespace Ajax\semantic\html\collections\form;
|
4 | 3 |
|
5 | 4 | use Ajax\JsUtils;
|
|
14 | 13 |
|
15 | 14 | class HtmlFormField extends HtmlSemDoubleElement {
|
16 | 15 | use FieldTrait;
|
| 16 | + |
17 | 17 | protected $_container;
|
| 18 | + |
18 | 19 | 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 !== "") |
24 | 29 | $this->setLabel($label);
|
25 | 30 | $this->setField($field);
|
26 | | - $this->_validation=NULL; |
| 31 | + $this->_validation = NULL; |
27 | 32 | }
|
28 | 33 |
|
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); |
31 | 36 | $labelO->setPointing($pointing);
|
32 | | - $this->addContent($labelO,$pointing==="below" || $pointing==="right"); |
| 37 | + $this->addContent($labelO,$pointing==="below" || $pointing==="right"); |
33 | 38 | return $labelO;
|
34 | 39 | }
|
35 | 40 |
|
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",""); |
40 | 45 | $labelO->setContent($label);
|
41 | | - $labelO->setProperty("for", \str_replace("field-", "",$this->identifier)); |
| 46 | + $labelO->setProperty("for", \str_replace("field-", "",$this->identifier)); |
42 | 47 | }
|
43 | | - $this->content["label"]=$labelO; |
| 48 | + $this->content["label"] = $labelO; |
44 | 49 | }
|
45 | 50 |
|
46 | | - public function setField($field){ |
47 | | - $this->content["field"]=$field; |
| 51 | + public function setField($field){ |
| 52 | + $this->content["field"] = $field; |
48 | 53 | }
|
49 | 54 |
|
50 | 55 | /**
|
51 | 56 | * Returns the label or null
|
| 57 | + * |
52 | 58 | * @return mixed
|
53 | 59 | */
|
54 | | - public function getLabel(){ |
55 | | - if(\array_key_exists("label", $this->content)) |
| 60 | + public function getLabel(){ |
| 61 | + if(\array_key_exists("label", $this->content)) |
56 | 62 | return $this->content["label"];
|
57 | 63 | }
|
58 | 64 |
|
59 | 65 | /**
|
60 | 66 | * Return the field
|
| 67 | + * |
61 | 68 | * @return mixed
|
62 | 69 | */
|
63 | | - public function getField(){ |
| 70 | + public function getField(){ |
64 | 71 | return $this->content["field"];
|
65 | 72 | }
|
66 | 73 |
|
67 | 74 | /**
|
68 | 75 | * Return the field with data
|
| 76 | + * |
69 | 77 | * @return mixed
|
70 | 78 | */
|
71 | | - public function getDataField(){ |
| 79 | + public function getDataField(){ |
72 | 80 | return $this->content["field"];
|
73 | 81 | }
|
74 | 82 |
|
75 | 83 | /**
|
76 | 84 | * puts the label before or behind
|
77 | 85 | */
|
78 | | - public function swapLabel(){ |
79 | | - $label=$this->getLabel(); |
| 86 | + public function swapLabel(){ |
| 87 | + $label = $this->getLabel(); |
80 | 88 | unset($this->content["label"]);
|
81 | | - $this->content["label"]=$label; |
| 89 | + $this->content["label"] = $label; |
82 | 90 | }
|
83 | 91 |
|
84 | 92 | /**
|
85 | 93 | * Defines the field width
|
| 94 | + * |
86 | 95 | * @param int $width
|
87 | 96 | * @return \Ajax\semantic\html\collections\form\HtmlFormField
|
88 | 97 | */
|
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]; |
92 | 101 | }
|
93 | 102 | $this->addToPropertyCtrl("class", $width, Wide::getConstants());
|
94 | | - if(isset($this->_container)){ |
| 103 | + if(isset($this->_container)){ |
95 | 104 | $this->_container->setEqualWidth(false);
|
96 | 105 | }
|
97 | | - return $this->addToPropertyCtrl("class", "wide",array("wide")); |
| 106 | + return $this->addToPropertyCtrl("class", "wide", array( |
| 107 | + "wide" |
| 108 | + )); |
98 | 109 | }
|
99 | 110 |
|
100 | 111 | /**
|
101 | 112 | * Field displays an error state
|
| 113 | + * |
102 | 114 | * @return \Ajax\semantic\html\collections\form\HtmlFormField
|
103 | 115 | */
|
104 | | - public function setError(){ |
| 116 | + public function setError(){ |
105 | 117 | return $this->addToProperty("class", "error");
|
106 | 118 | }
|
107 | 119 |
|
108 | | - public function setInline(){ |
| 120 | + public function setInline(){ |
109 | 121 | return $this->addToProperty("class", "inline");
|
110 | 122 | }
|
111 | 123 |
|
112 | | - public function jsState($state){ |
113 | | - return $this->jsDoJquery("addClass",$state); |
| 124 | + public function jsState($state){ |
| 125 | + return $this->jsDoJquery("addClass",$state); |
114 | 126 | }
|
115 | 127 |
|
116 | 128 | public function setContainer($_container) {
|
117 | | - $this->_container=$_container; |
| 129 | + $this->_container = $_container; |
118 | 130 | return $this;
|
119 | 131 | }
|
120 | 132 |
|
121 | | - public function setReadonly(){ |
| 133 | + public function setReadonly(){ |
122 | 134 | $this->getDataField()->setProperty("readonly", "");
|
123 | 135 | }
|
124 | 136 |
|
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()); |
130 | 142 | }
|
131 | | - if($type==="empty"){ |
132 | | - $this->addToProperty("class","required"); |
| 143 | + if($type==='empty' || ($type['type'] ?? '') === 'empty') { |
| 144 | + $this->addToProperty('class', 'required'); |
133 | 145 | }
|
134 | | - $this->_validation->addRule($type,$prompt,$value); |
| 146 | + $this->_validation->addRule($type,$prompt,$value); |
135 | 147 | }
|
136 | 148 | return $this;
|
137 | 149 | }
|
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()); |
144 | 156 | }
|
145 | 157 | $this->_validation->setOptional($optional);
|
146 | 158 | }
|
147 | 159 | }
|
148 | 160 |
|
149 | | - public function addRules(array $rules){ |
150 | | - foreach ($rules as $rule){ |
| 161 | + public function addRules(array $rules){ |
| 162 | + foreach ($rules as $rule){ |
151 | 163 | $this->addRule($rule);
|
152 | 164 | }
|
153 | 165 | return $this;
|
154 | 166 | }
|
155 | 167 |
|
156 | | - public function setRules(array $rules){ |
157 | | - $this->_validation=null; |
| 168 | + public function setRules(array $rules){ |
| 169 | + $this->_validation = null; |
158 | 170 | return $this->addRules($rules);
|
159 | 171 | }
|
160 | 172 |
|
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); |
164 | 176 | }
|
165 | 177 |
|
166 | 178 | public function getValidation() {
|
167 | 179 | return $this->_validation;
|
168 | 180 | }
|
169 | | - |
| 181 | + |
170 | 182 | public function setSize($size) {
|
171 | 183 | return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants());
|
172 | 184 | }
|
173 | 185 |
|
174 | 186 | public function run(JsUtils $js) {
|
175 | | - if(isset($this->_validation)){ |
| 187 | + if(isset($this->_validation)){ |
176 | 188 | $this->_validation->compile($js);
|
177 | 189 | }
|
178 | 190 | return parent::run($js);
|
179 | 191 | }
|
180 | | - |
181 | 192 | }
|
0 commit comments