Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Form_Error
  • Form_Field
  • Form_Form
  • Form_Input
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Class représentant un élément de formulaire
 15:  * @category Pry
 16:  * @package Form
 17:  * @version 1.0.7 
 18:  * @author Olivier ROGER <oroger.fr>
 19:  * @abstract
 20:  * @todo Gestion de plusieurs validateurs
 21:  *       
 22:  *
 23:  */
 24: abstract class Form_Field
 25: {
 26:     /**
 27:      * Formulaire
 28:      *
 29:      * @var Form_Form Formulaire
 30:      * @access protected
 31:      */
 32:     protected $form;
 33:     
 34:     /**
 35:      * Contient le label de l'élément
 36:      *
 37:      * @var string
 38:      * @access protected
 39:      */
 40:     protected $label;
 41:     
 42:     /**
 43:      * Class à appliquer aux label
 44:      *
 45:      * @var string
 46:      */
 47:     protected $cssLabel;
 48:     
 49:     /**
 50:      * Contient la valeur de l'élément
 51:      *
 52:      * @var string
 53:      * @access protected
 54:      */
 55:     protected $value;
 56:     
 57:     /**
 58:      * Classes CSS de l'élément
 59:      *
 60:      * @var array
 61:      * @access protected
 62:      */
 63:     protected $class;
 64:     
 65:     /**
 66:      * Liste des attributs de l'élément
 67:      *
 68:      * @var array
 69:      * @access protected
 70:      */
 71:     protected $attrs;
 72:     
 73:     /**
 74:      * Info à afficher dans un tooltips
 75:      *
 76:      * @var string
 77:      * @access protected
 78:      */
 79:     protected $info;
 80:     
 81:     /**
 82:      * Chemin de l'image servant au tooltips. 
 83:      * Chemin à donner par rapport à la page
 84:      *
 85:      * @var string
 86:      * @access protected
 87:      */
 88:     protected $imgInfo;
 89:     
 90:     /**
 91:      * Saut de ligne après élément
 92:      *
 93:      * @var boolean
 94:      * @access protected
 95:      */
 96:     protected $fieldNewLine;
 97:     
 98:     /**
 99:      * Saut de ligne après label
100:      *
101:      * @var booleang
102:      * @access protected
103:      */
104:     protected $labelNewLine;
105:     
106:     /**
107:      * Elément requis
108:      *
109:      * @var boolean
110:      * @access protected
111:      */
112:     protected $required;
113:     
114:     /**
115:      * Message d'erreur
116:      *
117:      * @var string
118:      * @access protected
119:      */
120:     protected $errorMsg;
121:     
122:     /**
123:      * Class CSS à utiliser pour le message d'erreur
124:      *
125:      * @var string
126:      */
127:     protected $errorClass;
128:     
129:     /**
130:      * Validateur
131:      *
132:      * @var Validate_Validate
133:      * @since 1.0.1
134:      */
135:     protected $validator;
136:     
137:     /**
138:      * Constructeur
139:      *
140:      * @param string $nom
141:      * @param Form_Form $form
142:      * @access protected
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:      * Assigne un label à l'élément
164:      *
165:      * @param string $txt
166:      * @param string $css à appliquer
167:      * @access public
168:      * @return Form_Field
169:      */
170:     public function label($txt,$css='')
171:     {
172:         $this->label    = $txt;
173:         $this->cssLabel = $css;
174:         return $this;
175:     }
176:     
177:     /**
178:      * Assigne une valeur à l'élément
179:      *
180:      * @param string $txt
181:      * @access public
182:      * @return Form_Field
183:      */
184:     public function value($txt='')
185:     {
186:         $this->value = $this->sanitizedValue($txt);
187:         return $this;
188:     }
189:     
190:     /**
191:      * Assigne une id
192:      *
193:      * @param string $txt
194:      * @access public
195:      * @return Form_Field
196:      */
197:     public function id($txt)
198:     {
199:         $this->attrs['id'] = $txt;
200:         return $this;
201:     }
202:     
203:     /**
204:      * Ajoute une classe CSS
205:      *
206:      * @param string $css
207:      * @access public
208:      * @return Form_Field
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:      * Ajoute un validateur
219:      *
220:      * @param string $nom Nom du validateur
221:      * @param array $options Option éventuelles
222:      * @param string $message Message personnalisé
223:      * @since 1.0.1
224:      * @return unknown
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:      * Défini l'élément comme requis ou non
237:      *
238:      * @param boolean $bool
239:      * @access public
240:      * @return Form_Field
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:      * Désactive ou non l'élément
258:      *
259:      * @param boolean $bool
260:      * @access public
261:      * @return Form_Field
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:      * Active ou non la lecture seule
274:      *
275:      * @param boolean $bool
276:      * @access public
277:      * @return Form_Field
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:      * Défini une longeur maxi pour la value
290:      *
291:      * @param string $val
292:      * @access public
293:      * @return Form_Field
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:      * Récupère le name de l'élément
307:      *
308:      * @access public
309:      * @return string
310:      */
311:     public function getName()
312:     {
313:         return $this->attrs['name'];
314:     }
315:     
316:     /**
317:      * Récupère la value de l élément
318:      *
319:      * @access public
320:      * @return string
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:      * Défini ou non un saut de ligne pour les label et les éléments
332:      *
333:      * @param boolean $label
334:      * @param boolean $field
335:      * @access public
336:      * @return Form_Field
337:      */
338:     public function newLine($label,$field)
339:     {
340:         $this->labelNewLine = $label;
341:         $this->fieldNewLine = $field;
342:         return $this;
343:     }
344:     
345:     /**
346:      * Nettoie la valeur recue
347:      *
348:      * @param string $value
349:      * @access public
350:      * @return string
351:      */
352:     public function sanitizedValue($value)
353:     {
354:         if(!is_array($value))
355:         {
356:             $value=trim($value);
357:             //Suppression des caractère non imprimable (on garde CR ,LF et TAB)
358:             $value= preg_replace('`[\x00\x08-\x0b\x0c\x0e\x19]`i', '', $value);
359:         }
360:         return $value;
361:     }
362:     
363:     /**
364:      * Valide l'élément
365:      *
366:      * @param string $valeur
367:      * @access public
368:      * @return boolean
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']])) // Utilisation du tableau de carac + sure que strlen à cause de l'encodage.
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:      * Défini un tooltip d'aide sur l'élément
411:      *
412:      * @param string $message
413:      * @return Form_Field
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:      * Défini une image pour illustrer les tooltips
424:      *
425:      * @param string $img Chemin vers l'image
426:      * @return Form_Field
427:      */
428:     public function setImgInfo($img)
429:     {
430:         $this->imgInfo = $img;
431:         return $this;
432:     }
433:     
434:     /**
435:      * Défini une class css d'erreur pour le msg d'erreur
436:      *
437:      * @param string $error
438:      * @access public
439:      * @return Form_Field
440:      */
441:     public function setErrorClass($error)
442:     {
443:         $this->errorClass = $error;
444:         return $this;
445:     }
446:     /**
447:      * Construit l'attribut HTML class=""
448:      *
449:      * @access protected
450:      * @return string
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:      * Défini un attribut de l'élément
464:      *
465:      * @param string $nom
466:      * @param string $valeur
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:      * Linéarise les attributs
477:      *
478:      * @access public
479:      * @return string
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:      * Ecrit l'objet
492:      * 
493:      * @abstract
494:      * @access private
495:      */
496:     abstract public function __toString();
497: }
498:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1