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
|
|
|
|
12
|
|
|
class HtmlFormField extends HtmlSemDoubleElement { |
13
|
|
|
protected $_container; |
14
|
|
|
protected $_validation; |
15
|
|
|
public function __construct($identifier, $field,$label=NULL) { |
16
|
|
|
parent::__construct($identifier, "div","field"); |
17
|
|
|
$this->content=array(); |
18
|
|
|
$this->_states=[State::ERROR,State::DISABLED]; |
19
|
|
|
if(isset($label)) |
20
|
|
|
$this->setLabel($label); |
21
|
|
|
$this->setField($field); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function addPointingLabel($label,$pointing=Direction::NONE){ |
25
|
|
|
$labelO=new HtmlLabel("",$label); |
26
|
|
|
$labelO->setPointing($pointing); |
27
|
|
|
$this->addContent($labelO,$pointing==="below" || $pointing==="right"); |
28
|
|
|
return $labelO; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setLabel($label){ |
32
|
|
|
$labelO=$label; |
33
|
|
|
if(\is_string($label)){ |
34
|
|
|
$labelO=new HtmlSemDoubleElement("","label",""); |
35
|
|
|
$labelO->setContent($label); |
36
|
|
|
$labelO->setProperty("for", \str_replace("field-", "",$this->identifier)); |
37
|
|
|
} |
38
|
|
|
$this->content["label"]=$labelO; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setField($field){ |
42
|
|
|
$this->content["field"]=$field; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the label or null |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function getLabel(){ |
50
|
|
|
if(\array_key_exists("label", $this->content)) |
51
|
|
|
return $this->content["label"]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return the field |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function getField(){ |
59
|
|
|
return $this->content["field"]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* puts the label before or behind |
64
|
|
|
*/ |
65
|
|
|
public function swapLabel(){ |
66
|
|
|
$label=$this->getLabel(); |
67
|
|
|
unset($this->content["label"]); |
68
|
|
|
$this->content["label"]=$label; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Defines the field width |
73
|
|
|
* @param int $width |
74
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
75
|
|
|
*/ |
76
|
|
|
public function setWidth($width){ |
77
|
|
|
if(\is_int($width)){ |
78
|
|
|
$width=Wide::getConstants()["W".$width]; |
79
|
|
|
} |
80
|
|
|
$this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
81
|
|
|
if(isset($this->_container)){ |
82
|
|
|
$this->_container->setEqualWidth(false); |
83
|
|
|
} |
84
|
|
|
return $this->addToPropertyCtrl("class", "wide",array("wide")); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Field displays an error state |
89
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
90
|
|
|
*/ |
91
|
|
|
public function setError(){ |
92
|
|
|
return $this->addToProperty("class", "error"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setInline(){ |
96
|
|
|
return $this->addToProperty("class", "inline"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function jsState($state){ |
100
|
|
|
return $this->jsDoJquery("addClass",$state); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setContainer($_container) { |
104
|
|
|
$this->_container=$_container; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setReadonly(){ |
109
|
|
|
$this->getField()->setProperty("readonly", ""); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function addRule($type,$prompt=NULL,$value=NULL){ |
113
|
|
|
$field=$this->getField(); |
114
|
|
|
if(isset($field)){ |
115
|
|
|
if(!isset($this->_validation)){ |
116
|
|
|
$this->_validation=new FieldValidation($field->getIdentifier()); |
117
|
|
|
} |
118
|
|
|
$this->_validation->addRule($type,$prompt,$value); |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getValidation() { |
124
|
|
|
return $this->_validation; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
} |