1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\form; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
7
|
|
|
use Ajax\semantic\html\base\constants\State; |
8
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
9
|
|
|
use Ajax\semantic\html\elements\HtmlLabel; |
10
|
|
|
use Ajax\semantic\components\validation\FieldValidation; |
11
|
|
|
use Ajax\semantic\html\collections\form\traits\FieldTrait; |
12
|
|
|
|
13
|
|
|
class HtmlFormField extends HtmlSemDoubleElement { |
14
|
|
|
use FieldTrait; |
15
|
|
|
protected $_container; |
16
|
|
|
protected $_validation; |
17
|
|
|
public function __construct($identifier, $field,$label=NULL) { |
18
|
|
|
parent::__construct($identifier, "div","field"); |
19
|
|
|
$this->content=array(); |
20
|
|
|
$this->_states=[State::ERROR,State::DISABLED]; |
21
|
|
|
if(isset($label) && $label!=="") |
22
|
|
|
$this->setLabel($label); |
23
|
|
|
$this->setField($field); |
24
|
|
|
$this->_validation=NULL; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function addPointingLabel($label,$pointing=Direction::NONE){ |
28
|
|
|
$labelO=new HtmlLabel("",$label); |
29
|
|
|
$labelO->setPointing($pointing); |
30
|
|
|
$this->addContent($labelO,$pointing==="below" || $pointing==="right"); |
31
|
|
|
return $labelO; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setLabel($label){ |
35
|
|
|
$labelO=$label; |
36
|
|
|
if(\is_string($label)){ |
37
|
|
|
$labelO=new HtmlSemDoubleElement("","label",""); |
38
|
|
|
$labelO->setContent($label); |
39
|
|
|
$labelO->setProperty("for", \str_replace("field-", "",$this->identifier)); |
40
|
|
|
} |
41
|
|
|
$this->content["label"]=$labelO; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setField($field){ |
45
|
|
|
$this->content["field"]=$field; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the label or null |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getLabel(){ |
53
|
|
|
if(\array_key_exists("label", $this->content)) |
54
|
|
|
return $this->content["label"]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the field |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getField(){ |
62
|
|
|
return $this->content["field"]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the field with data |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function getDataField(){ |
70
|
|
|
return $this->content["field"]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* puts the label before or behind |
75
|
|
|
*/ |
76
|
|
|
public function swapLabel(){ |
77
|
|
|
$label=$this->getLabel(); |
78
|
|
|
unset($this->content["label"]); |
79
|
|
|
$this->content["label"]=$label; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Defines the field width |
84
|
|
|
* @param int $width |
85
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
86
|
|
|
*/ |
87
|
|
|
public function setWidth($width){ |
88
|
|
|
if(\is_int($width)){ |
|
|
|
|
89
|
|
|
$width=Wide::getConstants()["W".$width]; |
90
|
|
|
} |
91
|
|
|
$this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
92
|
|
|
if(isset($this->_container)){ |
93
|
|
|
$this->_container->setEqualWidth(false); |
94
|
|
|
} |
95
|
|
|
return $this->addToPropertyCtrl("class", "wide",array("wide")); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Field displays an error state |
100
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
101
|
|
|
*/ |
102
|
|
|
public function setError(){ |
103
|
|
|
return $this->addToProperty("class", "error"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function setInline(){ |
107
|
|
|
return $this->addToProperty("class", "inline"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function jsState($state){ |
111
|
|
|
return $this->jsDoJquery("addClass",$state); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setContainer($_container) { |
115
|
|
|
$this->_container=$_container; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function setReadonly(){ |
120
|
|
|
$this->getField()->setProperty("readonly", ""); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function addRule($type,$prompt=NULL,$value=NULL){ |
124
|
|
|
$field=$this->getDataField(); |
125
|
|
|
if(isset($field)){ |
126
|
|
|
if(!isset($this->_validation)){ |
127
|
|
|
$this->_validation=new FieldValidation($field->getIdentifier()); |
128
|
|
|
} |
129
|
|
|
if($type==="empty"){ |
130
|
|
|
$this->addToProperty("class","required"); |
131
|
|
|
} |
132
|
|
|
$this->_validation->addRule($type,$prompt,$value); |
133
|
|
|
} |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setOptional($optional=true){ |
138
|
|
|
$field=$this->getDataField(); |
139
|
|
|
if(isset($field)){ |
140
|
|
|
if(!isset($this->_validation)){ |
141
|
|
|
$this->_validation=new FieldValidation($field->getIdentifier()); |
142
|
|
|
} |
143
|
|
|
$this->_validation->setOptional($optional); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function addRules(array $rules){ |
148
|
|
|
foreach ($rules as $rule){ |
149
|
|
|
$this->addRule($rule); |
150
|
|
|
} |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setRules(array $rules){ |
155
|
|
|
$this->_validation=null; |
156
|
|
|
return $this->addRules($rules); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function addIcon($icon,$direction=Direction::LEFT){ |
160
|
|
|
$field=$this->getField(); |
161
|
|
|
return $field->addIcon($icon,$direction); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getValidation() { |
165
|
|
|
return $this->_validation; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|