Completed
Push — master ( 9281ef...7089ac )
by Jean-Christophe
03:28
created

HtmlFormField::addRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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