1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Form;
14:
15: use Pry\Validate\Validate;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: abstract class Field
29: {
30:
31: 32: 33: 34: 35: 36:
37: protected $form;
38:
39: 40: 41: 42: 43: 44:
45: protected $label;
46:
47: 48: 49: 50: 51:
52: protected $cssLabel;
53:
54: 55: 56: 57: 58: 59:
60: protected $value;
61:
62: 63: 64: 65: 66: 67:
68: protected $class;
69:
70: 71: 72: 73: 74: 75:
76: protected $attrs;
77:
78: 79: 80: 81: 82: 83:
84: protected $info;
85:
86: 87: 88: 89: 90: 91: 92:
93: protected $imgInfo;
94:
95: 96: 97: 98: 99: 100:
101: protected $fieldNewLine;
102:
103: 104: 105: 106: 107: 108:
109: protected $labelNewLine;
110:
111: 112: 113: 114: 115: 116:
117: protected $required;
118:
119: 120: 121: 122: 123: 124:
125: protected $errorMsg;
126:
127: 128: 129: 130: 131:
132: protected $errorClass;
133:
134: 135: 136: 137: 138: 139:
140: protected $validator;
141:
142: 143: 144: 145: 146: 147: 148:
149: protected function __construct($nom, $form)
150: {
151: $this->form = $form;
152: $this->label = '';
153: $this->cssLabel = '';
154: $this->value = '';
155: $this->info = '';
156: $this->imgInfo = 'data:image/gif;base64,R0lGODlhEAAQAPcAAAAAAP///zNVnzVWnjRTlTRQkjRSkjVRkDRVmTRQijRQiTRPiDVQiTRPhjVPhDVrsTVmq1Z7rp264KvC4a7F4/T3+zVpq1J6q1eAsl6FtF6Es16Fs12DsWOLu2OKuWCGtGmRwneg0JOz242qzZ+84J+836K93bTL57PK5rjN5l+Jt1yDsGSPvmKMumaQv2WPvmKKuGKKtmyYx2yWxXGdzGmSvnKezXGcy3Sez3Wgz3ql1Hqj0HKWvoKp1nidxYWr14mt1ZGz2Zm11Jez0aO/3aXA36jE4qK82qjD4ajC4LDJ5K/H47jL4dPf7dDc6tTg7luKuWeTwWmWxGmVw2mWw2iUwmiUwWaRvmyax2qXxGuYxWuYxHGdy3Gey22Yw3ek0Xai0Hml0XehzX2p1necw4Wr0Yyy2aC/4KK/3Z+72KvF3/X4+/T3+vz9/jV+vzV9vUWMzUeNz0eOzkmR0UyS0mKXyF6Rv2iczWqg0GuZxYGqz4au1IWs0Jy826rG3zV+vDuFxUGLy0CLyX2ny5C00pO31Ze62KbD20GOy3ejxXmlxqbD2pO40v7//////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAI4ALAAAAAAQABAAAAjFAB0JHEiwoEFHdOAEEnRw4Bw8ZyhQKFHnzcE4ZlJI+NEjyIQ9DwrKMYNiTBgjSMB8McEHAsE7J3SEwLFmTRcuYopAGYiIhIgcNm4QQZMnz5YyhgYIBKRkBw0sWgI0omKlihc1BAS6WQImTxYpTyq4eMGihp8CAv/0ASJjSpQmbDp4gOGD0YGBdpLMAMGkTSMnGTYIiUDQgp4jV1qo0MBhxQhFCgoKGJSGTIwPPIYkcnAQAYZChxYRutCgoUADCRYwMM16YEAAOw==';
157: $this->class = array();
158: $this->attrs = array();
159: $this->validators = array();
160: $this->required = true;
161: $this->fieldNewLine = true;
162: $this->labelNewLine = true;
163: $this->errorMsg = null;
164: $this->errorClass = 'errorForm';
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174:
175: public function label($txt, $css = '')
176: {
177: $this->label = $txt;
178: $this->cssLabel = $css;
179: return $this;
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: public function value($txt = '')
190: {
191: $this->value = $this->sanitizedValue($txt);
192: return $this;
193: }
194:
195: 196: 197: 198: 199: 200: 201:
202: public function id($txt)
203: {
204: $this->attrs['id'] = $txt;
205: return $this;
206: }
207:
208: 209: 210: 211: 212: 213: 214:
215: public function addClass($css)
216: {
217: if (!in_array($css, $this->class))
218: $this->class[] = $css;
219: return $this;
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229: 230:
231: public function addValidator($nom, $options = null, $message = '')
232: {
233: if (!is_object($this->validator))
234: {
235: $this->validator = new Validate();
236: }
237: $this->validator->addValidator($nom, $options, $message);
238: return $this;
239: }
240:
241: 242: 243: 244: 245: 246: 247:
248: public function required($bool = true)
249: {
250: if ($bool === true)
251: {
252: $this->required = true;
253: }
254: else
255: {
256: unset($this->attrs['required']);
257: $this->required = false;
258: }
259: return $this;
260: }
261:
262: 263: 264: 265: 266: 267: 268:
269: public function disabled($bool = true)
270: {
271: if ($bool === true)
272: $this->attrs['disabled'] = 'disabled';
273: else
274: unset($this->attrs['disabled']);
275: return $this;
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: public function readonly($bool = true)
286: {
287: if ($bool === true)
288: $this->attrs['readonly'] = 'readonly';
289: else
290: unset($this->attrs['readonly']);
291: return $this;
292: }
293:
294: 295: 296: 297: 298: 299: 300:
301: public function maxlength($val)
302: {
303: if (ctype_digit((string) $val) && $val > 0)
304: $this->attrs['maxlength'] = $val;
305: else
306: unset($this->attrs['maxlength']);
307:
308: return $this;
309: }
310:
311: 312: 313: 314: 315: 316:
317: public function getName()
318: {
319: return $this->attrs['name'];
320: }
321:
322: 323: 324: 325: 326: 327:
328: public function getValue()
329: {
330: if (isset($this->attrs['value']))
331: return $this->attrs['value'];
332: else
333: return '';
334: }
335:
336: 337: 338: 339: 340: 341: 342: 343:
344: public function newLine($label, $field)
345: {
346: $this->labelNewLine = $label;
347: $this->fieldNewLine = $field;
348: return $this;
349: }
350:
351: 352: 353: 354: 355: 356: 357:
358: public function sanitizedValue($value)
359: {
360: if (!is_array($value))
361: {
362: $value = trim($value);
363:
364: $value = preg_replace('`[\x00\x08-\x0b\x0c\x0e\x19]`i', '', $value);
365: }
366: return $value;
367: }
368:
369: 370: 371: 372: 373: 374: 375:
376: public function isValid($valeur)
377: {
378: $valeur = $this->sanitizedValue($valeur);
379: if (!$this->required && empty($valeur))
380: return true;
381:
382: if (is_object($this->validator))
383: {
384: $validation = $this->validator->isValid($valeur);
385: if ($validation !== true)
386: {
387: $this->errorMsg = $validation;
388: return false;
389: }
390: }
391:
392: if ($this->required && $valeur != '')
393: {
394: if (isset($this->attrs['maxlength']))
395: {
396: if (isset($valeur[$this->attrs['maxlength']]))
397: {
398: $this->errorMsg = Error::TOOLONG;
399: return false;
400: }
401: }
402: return true;
403: }
404: elseif (!$this->required)
405: {
406: return true;
407: }
408: else
409: {
410: $this->errorMsg = Error::REQUIRED;
411: return false;
412: }
413: }
414:
415: 416: 417: 418: 419: 420:
421: public function info($message)
422: {
423: $this->info = $message;
424: $this->form->listTooltips[] = $this->attrs['name'];
425: return $this;
426: }
427:
428: 429: 430: 431: 432: 433:
434: public function setImgInfo($img)
435: {
436: $this->imgInfo = $img;
437: return $this;
438: }
439:
440: 441: 442: 443: 444: 445: 446:
447: public function setErrorClass($error)
448: {
449: $this->errorClass = $error;
450: return $this;
451: }
452:
453: 454: 455: 456: 457: 458:
459: protected function cssClass()
460: {
461: $css = implode(' ', $this->class);
462: if ($css != '')
463: $css = 'class="' . $css . '"';
464: else
465: $css = '';
466: return $css;
467: }
468:
469: 470: 471: 472: 473: 474:
475: public function setAttributes($nom, $valeur)
476: {
477: if (!isset($this->attrs[$nom]))
478: $this->attrs[$nom] = $valeur;
479: return $this;
480: }
481:
482: 483: 484: 485: 486: 487:
488: protected function attrsToString()
489: {
490: $attributs = '';
491: foreach ($this->attrs as $key => $value)
492: $attributs .= $key . '="' . $value . '" ';
493:
494: return $attributs;
495: }
496:
497: 498: 499: 500: 501: 502:
503: abstract public function __toString();
504: }
505:
506: ?>