phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlFormField.php
Go to the documentation of this file.
1 <?php
3 
4 use Ajax\JsUtils;
14 
16  use FieldTrait;
17 
18  protected $_container;
19 
20  protected $_validation;
21 
22  public function __construct($identifier, $field, $label = NULL) {
23  parent::__construct($identifier, "div", "field");
24  $this->content = array();
25  $this->_states = [
28  ];
29  if (isset($label) && $label !== "")
30  $this->setLabel($label);
31  $this->setField($field);
32  $this->_validation = NULL;
33  }
34 
35  public function addPointingLabel($label, $pointing = Direction::NONE) {
36  $labelO = new HtmlLabel("", $label);
37  $labelO->setPointing($pointing);
38  $this->addContent($labelO, $pointing === "below" || $pointing === "right");
39  return $labelO;
40  }
41 
42  public function setLabel($label) {
43  $labelO = $label;
44  if (\is_string($label)) {
45  $labelO = new HtmlSemDoubleElement("", "label", "");
46  $labelO->setContent($label);
47  $labelO->setProperty("for", \str_replace("field-", "", $this->identifier));
48  }
49  $this->content["label"] = $labelO;
50  }
51 
52  public function setField($field) {
53  $this->content["field"] = $field;
54  }
55 
61  public function getLabel() {
62  if (\array_key_exists("label", $this->content))
63  return $this->content["label"];
64  }
65 
71  public function getField() {
72  return $this->content["field"];
73  }
74 
80  public function getDataField() {
81  return $this->content["field"];
82  }
83 
87  public function swapLabel() {
88  $label = $this->getLabel();
89  unset($this->content["label"]);
90  $this->content["label"] = $label;
91  }
92 
99  public function setWidth($width) {
100  if (\is_int($width)) {
101  $width = Wide::getConstants()["W" . $width];
102  }
103  $this->addToPropertyCtrl("class", $width, Wide::getConstants());
104  if (isset($this->_container)) {
105  $this->_container->setEqualWidth(false);
106  }
107  return $this->addToPropertyCtrl("class", "wide", array(
108  "wide"
109  ));
110  }
111 
117  public function setError() {
118  return $this->addToProperty("class", "error");
119  }
120 
121  public function setInline() {
122  return $this->addToProperty("class", "inline");
123  }
124 
125  public function jsState($state) {
126  return $this->jsDoJquery("addClass", $state);
127  }
128 
129  public function setContainer($_container) {
130  $this->_container = $_container;
131  return $this;
132  }
133 
134  public function setReadonly() {
135  $this->getDataField()->setProperty("readonly", "");
136  }
137 
138  public function addRule($type, $prompt = NULL, $value = NULL) {
139  $field = $this->getDataField();
140  if (isset($field)) {
141  if (! isset($this->_validation)) {
142  $this->_validation = new FieldValidation($field->getIdentifier());
143  }
144  if($type instanceof Rule){
145  if($type->getType()=='empty'){
146  $this->addToProperty('class', 'required');
147  }
148  }elseif ($type === 'empty' || ($type['type'] ?? '') === 'empty') {
149  $this->addToProperty('class', 'required');
150  }
151  $this->_validation->addRule($type, $prompt, $value);
152  }
153  return $this;
154  }
155 
156  public function setOptional($optional = true) {
157  $field = $this->getDataField();
158  if (isset($field)) {
159  if (! isset($this->_validation)) {
160  $this->_validation = new FieldValidation($field->getIdentifier());
161  }
162  $this->_validation->setOptional($optional);
163  }
164  }
165 
166  public function addRules(array $rules) {
167  foreach ($rules as $rule) {
168  $this->addRule($rule);
169  }
170  return $this;
171  }
172 
173  public function setRules(array $rules) {
174  $this->_validation = null;
175  return $this->addRules($rules);
176  }
177 
178  public function addIcon($icon, $direction = Direction::LEFT) {
179  $field = $this->getField();
180  return $field->addIcon($icon, $direction);
181  }
182 
183  public function getValidation() {
184  return $this->_validation;
185  }
186 
187  public function setSize($size) {
188  return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants());
189  }
190 
191  public function run(JsUtils $js) {
192  if (isset($this->_validation)) {
193  $this->_validation->compile($js);
194  }
195  return parent::run($js);
196  }
197 }
addToProperty($name, $value, $separator=" ")
setWidth($width)
Defines the field width.
static getConstants()
Definition: BaseEnum.php:17
Semantic Label component.
Definition: HtmlLabel.php:20
addRule($type, $prompt=NULL, $value=NULL)
addContent($content, $before=false)
swapLabel()
puts the label before or behind
jsDoJquery($jqueryCall, $param="")
addIcon($icon, $direction=Direction::LEFT)
Base class for Semantic double elements.
JQuery PHP library.
Definition: JsUtils.php:23
addToPropertyCtrl($name, $value, $typeCtrl)
__construct($identifier, $field, $label=NULL)
setError()
Field displays an error state.
addPointingLabel($label, $pointing=Direction::NONE)