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