1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract form fields |
4
|
|
|
* |
5
|
|
|
* Ntentan Framework |
6
|
|
|
* Copyright (c) 2008-2013 James Ekow Abaka Ainooson |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
9
|
|
|
* a copy of this software and associated documentation files (the |
10
|
|
|
* "Software"), to deal in the Software without restriction, including |
11
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
12
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
13
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
14
|
|
|
* the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be |
17
|
|
|
* included in all copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
20
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
21
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
22
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
23
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
24
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
25
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
26
|
|
|
* |
27
|
|
|
* @author James Ainooson <[email protected]> |
28
|
|
|
* @copyright Copyright 2010 James Ekow Abaka Ainooson |
29
|
|
|
* @license MIT |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The form field class. This class represents a form field element. |
37
|
|
|
* Sublcasses of this class are to be used to capture information from |
38
|
|
|
* the user of the application. |
39
|
|
|
* \ingroup Form_API |
40
|
|
|
*/ |
41
|
|
|
class Field extends Element |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* A flag for setting the required state of the form. If this value |
45
|
|
|
* is set as true then the form would not be validated if there is |
46
|
|
|
* no value entered into this field. |
47
|
|
|
*/ |
48
|
|
|
public $required = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The value of the form field. |
52
|
|
|
*/ |
53
|
|
|
protected $value; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The constructor for the field element. |
57
|
|
|
*/ |
58
|
|
|
public function __construct($name="", $value="") |
59
|
|
|
{ |
60
|
|
|
$this->name = $name; |
61
|
|
|
$this->value = $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the value of the field. |
66
|
|
|
* |
67
|
|
|
* @param $value The value of the field. |
68
|
|
|
* @return Field |
69
|
|
|
*/ |
70
|
|
|
public function setValue($value) |
71
|
|
|
{ |
72
|
|
|
$this->value = $value; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the value of the field. |
78
|
|
|
* |
79
|
|
|
* @return unknown |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function getValue() |
82
|
|
|
{ |
83
|
|
|
return $this->value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the required status of the field. |
88
|
|
|
* |
89
|
|
|
* @param The required status of the field. |
90
|
|
|
*/ |
91
|
|
|
public function setRequired($required) |
92
|
|
|
{ |
93
|
|
|
$this->required = $required; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the required status of the field. |
99
|
|
|
* |
100
|
|
|
* @return The required status of the field. |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
public function getRequired() |
103
|
|
|
{ |
104
|
|
|
return $this->required; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//! Sets the data that is stored in this field. |
108
|
|
|
//! \param $data An array of fields. This method just looks through for |
109
|
|
|
//! a field that matches it and then applies its value to |
110
|
|
|
//! itself. |
111
|
|
|
public function setData($data) |
112
|
|
|
{ |
113
|
|
|
if(array_search($this->getName(false),array_keys($data))!==false) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->setValue($data[$this->getName(false)]); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getType() |
120
|
|
|
{ |
121
|
|
|
return __CLASS__; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getCSSClasses() |
125
|
|
|
{ |
126
|
|
|
$classes = parent::getCSSClasses(); |
127
|
|
|
if($this->error) $classes.="error "; |
128
|
|
|
if($this->getRequired()) $classes .="required "; |
129
|
|
|
return trim($classes); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function render() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}"); |
135
|
|
|
$this->setAttribute("name", $this->getName()); |
136
|
|
|
return TemplateEngine::render("input_element.tpl.php", array('element' => $this)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.