nyroFwk  0.2
form/abstract.class.php
Go to the documentation of this file.
1 <?php
10 abstract class form_abstract extends object {
11 
17  protected $valid;
18 
24  protected $customErrors = array();
25 
29  protected function afterInit() {
30  if (!$this->label && !is_bool($this->label))
31  $this->label = ucfirst($this->name);
32  if (!is_object($this->cfg->value))
33  $this->cfg->value = utils::htmlOut($this->cfg->value);
34  $this->id = $this->makeId($this->name);
35  $val = &$this->cfg->getRef('value');
36  $this->valid = factory::get($this->cfg->validType, array(
37  'value'=>&$val,
38  'label'=>$this->label,
39  'validEltArray'=>$this->cfg->getInArray('valid', 'validEltArray'),
40  ));
41  $this->cfg->delInArray('valid', 'validEltArray');
42  $this->initValid();
43  }
44 
45  public function renew() {
46  $this->id = $this->makeId($this->name);
47  }
48 
55  protected function makeId($name) {
56  return str_replace(
57  array('[]', '[', ']'),
58  array('_', '_', ''),
59  $name);
60  }
66  public function getName() {
67  return $this->cfg->name;
68  }
69 
75  public function getValue() {
76  return utils::htmlDeOut($this->cfg->value);
77  }
78 
84  public function &getRawValue() {
85  return $this->cfg->getRef('value');
86  }
87 
94  public function setValue($value, $refill=false) {
95  if ($this->cfg->disabled)
96  return;
97  $this->cfg->set('value', utils::htmlOut($value));
98  }
99 
105  public function getDescription() {
106  $ret = $this->cfg->description;
107  if ($this->cfg->outDescription)
108  $ret = utils::htmlOut($ret);
109  return $ret;
110  }
111 
115  protected function initValid() {
116  $valid = $this->cfg->valid;
117  if (is_array($valid)) {
118  foreach($valid as $type=>$prm)
119  if (is_int($type))
120  $this->addRule($prm);
121  else if ($prm)
122  $this->addRule($type, $prm);
123  } else
124  $this->addRule($valid);
125  }
126 
132  public function getValid() {
133  return $this->valid;
134  }
135 
141  public function isValid() {
142  return $this->valid->isValid() && empty($this->customErrors);
143  }
144 
151  public function getValidRule($name) {
152  if (!$this->valid)
153  return null;
154  $tmp = $this->valid->getRules();
155  return array_key_exists($name, $tmp) ? $tmp[$name] : null;
156  }
157 
163  public function setDisabled($disabled) {
164  $this->cfg->disabled = $disabled;
165  if ($disabled)
166  $this->cfg->setInArray('html', 'disabled', 'disabled');
167  else
168  $this->cfg->delInArray('html', 'disabled');
169  }
170 
176  public function getErrors() {
177  return array_merge_recursive($this->valid->getErrors(), $this->customErrors);
178  }
179 
185  public function addCustomError($error) {
186  $this->customErrors[] = $error;
187  }
188 
195  public function addRule($type, $prm=null) {
196  $this->valid->addRule($type, $prm);
197  }
198 
204  public function delRule($type) {
205  $this->valid->delRule($type);
206  }
207 
213  public function isHidden() {
214  return false;
215  }
216 
222  public function getType() {
223  return substr(get_class($this), strlen('form_'));
224  }
225 
232  public function to($type) {
233  return $this->{'to'.ucfirst($type)}();
234  }
235 
239  abstract public function toHtml();
240 
244  public function toXul() {
245  return '';
246  }
247 
253  public function __toString() {
254  return $this->to(request::get('out'));
255  }
256 
263  public function __get($name) {
264  return $this->cfg->get($name);
265  }
266 
273  public function __set($name, $val) {
274  $ret = $this->cfg->set($name, $val);
275  if ($name == 'label' && $this->getValid())
276  $this->getValid()->getCfg()->label = $val;
277  return $ret;
278  }
279 
280 }
static get($get=null)
static htmlOut($val, $key=false)
static htmlDeOut($val, $key=false)
addRule($type, $prm=null)
isValid()
Definition: valid.class.php:85
delRule($type)
Definition: valid.class.php:67
getRules()
Definition: valid.class.php:76
addRule($type, $prm=null, $msg=null)
Definition: valid.class.php:44
setValue($value, $refill=false)
getErrors()
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13