Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Error
  • Field
  • Form
  • Input
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Form;
 14: 
 15: use Pry\Validate\Validate;
 16: 
 17: /**
 18:  * Class représentant un élément de formulaire
 19:  * @category Pry
 20:  * @package Form
 21:  * @version 1.0.7 
 22:  * @author Olivier ROGER <oroger.fr>
 23:  * @abstract
 24:  * @todo Gestion de plusieurs validateurs
 25:  *       
 26:  *
 27:  */
 28: abstract class Field
 29: {
 30: 
 31:     /**
 32:      * Formulaire
 33:      *
 34:      * @var Form_Form Formulaire
 35:      * @access protected
 36:      */
 37:     protected $form;
 38: 
 39:     /**
 40:      * Contient le label de l'élément
 41:      *
 42:      * @var string
 43:      * @access protected
 44:      */
 45:     protected $label;
 46: 
 47:     /**
 48:      * Class à appliquer aux label
 49:      *
 50:      * @var string
 51:      */
 52:     protected $cssLabel;
 53: 
 54:     /**
 55:      * Contient la valeur de l'élément
 56:      *
 57:      * @var string
 58:      * @access protected
 59:      */
 60:     protected $value;
 61: 
 62:     /**
 63:      * Classes CSS de l'élément
 64:      *
 65:      * @var array
 66:      * @access protected
 67:      */
 68:     protected $class;
 69: 
 70:     /**
 71:      * Liste des attributs de l'élément
 72:      *
 73:      * @var array
 74:      * @access protected
 75:      */
 76:     protected $attrs;
 77: 
 78:     /**
 79:      * Info à afficher dans un tooltips
 80:      *
 81:      * @var string
 82:      * @access protected
 83:      */
 84:     protected $info;
 85: 
 86:     /**
 87:      * Chemin de l'image servant au tooltips. 
 88:      * Chemin à donner par rapport à la page
 89:      *
 90:      * @var string
 91:      * @access protected
 92:      */
 93:     protected $imgInfo;
 94: 
 95:     /**
 96:      * Saut de ligne après élément
 97:      *
 98:      * @var boolean
 99:      * @access protected
100:      */
101:     protected $fieldNewLine;
102: 
103:     /**
104:      * Saut de ligne après label
105:      *
106:      * @var booleang
107:      * @access protected
108:      */
109:     protected $labelNewLine;
110: 
111:     /**
112:      * Elément requis
113:      *
114:      * @var boolean
115:      * @access protected
116:      */
117:     protected $required;
118: 
119:     /**
120:      * Message d'erreur
121:      *
122:      * @var string
123:      * @access protected
124:      */
125:     protected $errorMsg;
126: 
127:     /**
128:      * Class CSS à utiliser pour le message d'erreur
129:      *
130:      * @var string
131:      */
132:     protected $errorClass;
133: 
134:     /**
135:      * Validateur
136:      *
137:      * @var Validate_Validate
138:      * @since 1.0.1
139:      */
140:     protected $validator;
141: 
142:     /**
143:      * Constructeur
144:      *
145:      * @param string $nom
146:      * @param Form_Form $form
147:      * @access protected
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:      * Assigne un label à l'élément
169:      *
170:      * @param string $txt
171:      * @param string $css à appliquer
172:      * @access public
173:      * @return Form_Field
174:      */
175:     public function label($txt, $css = '')
176:     {
177:         $this->label    = $txt;
178:         $this->cssLabel = $css;
179:         return $this;
180:     }
181: 
182:     /**
183:      * Assigne une valeur à l'élément
184:      *
185:      * @param string $txt
186:      * @access public
187:      * @return Form_Field
188:      */
189:     public function value($txt = '')
190:     {
191:         $this->value = $this->sanitizedValue($txt);
192:         return $this;
193:     }
194: 
195:     /**
196:      * Assigne une id
197:      *
198:      * @param string $txt
199:      * @access public
200:      * @return Form_Field
201:      */
202:     public function id($txt)
203:     {
204:         $this->attrs['id'] = $txt;
205:         return $this;
206:     }
207: 
208:     /**
209:      * Ajoute une classe CSS
210:      *
211:      * @param string $css
212:      * @access public
213:      * @return Form_Field
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:      * Ajoute un validateur
224:      *
225:      * @param string $nom Nom du validateur
226:      * @param array $options Option éventuelles
227:      * @param string $message Message personnalisé
228:      * @since 1.0.1
229:      * @return unknown
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:      * Défini l'élément comme requis ou non
243:      *
244:      * @param boolean $bool
245:      * @access public
246:      * @return Form_Field
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:      * Désactive ou non l'élément
264:      *
265:      * @param boolean $bool
266:      * @access public
267:      * @return Form_Field
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:      * Active ou non la lecture seule
280:      *
281:      * @param boolean $bool
282:      * @access public
283:      * @return Form_Field
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:      * Défini une longeur maxi pour la value
296:      *
297:      * @param string $val
298:      * @access public
299:      * @return Form_Field
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:      * Récupère le name de l'élément
313:      *
314:      * @access public
315:      * @return string
316:      */
317:     public function getName()
318:     {
319:         return $this->attrs['name'];
320:     }
321: 
322:     /**
323:      * Récupère la value de l élément
324:      *
325:      * @access public
326:      * @return string
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:      * Défini ou non un saut de ligne pour les label et les éléments
338:      *
339:      * @param boolean $label
340:      * @param boolean $field
341:      * @access public
342:      * @return Form_Field
343:      */
344:     public function newLine($label, $field)
345:     {
346:         $this->labelNewLine = $label;
347:         $this->fieldNewLine = $field;
348:         return $this;
349:     }
350: 
351:     /**
352:      * Nettoie la valeur recue
353:      *
354:      * @param string $value
355:      * @access public
356:      * @return string
357:      */
358:     public function sanitizedValue($value)
359:     {
360:         if (!is_array($value))
361:         {
362:             $value = trim($value);
363:             //Suppression des caractère non imprimable (on garde CR ,LF et TAB)
364:             $value = preg_replace('`[\x00\x08-\x0b\x0c\x0e\x19]`i', '', $value);
365:         }
366:         return $value;
367:     }
368: 
369:     /**
370:      * Valide l'élément
371:      *
372:      * @param string $valeur
373:      * @access public
374:      * @return boolean
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:                 { // Utilisation du tableau de carac + sure que strlen à cause de l'encodage.
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:      * Défini un tooltip d'aide sur l'élément
417:      *
418:      * @param string $message
419:      * @return Form_Field
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:      * Défini une image pour illustrer les tooltips
430:      *
431:      * @param string $img Chemin vers l'image
432:      * @return Form_Field
433:      */
434:     public function setImgInfo($img)
435:     {
436:         $this->imgInfo = $img;
437:         return $this;
438:     }
439: 
440:     /**
441:      * Défini une class css d'erreur pour le msg d'erreur
442:      *
443:      * @param string $error
444:      * @access public
445:      * @return Form_Field
446:      */
447:     public function setErrorClass($error)
448:     {
449:         $this->errorClass = $error;
450:         return $this;
451:     }
452: 
453:     /**
454:      * Construit l'attribut HTML class=""
455:      *
456:      * @access protected
457:      * @return string
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:      * Défini un attribut de l'élément
471:      *
472:      * @param string $nom
473:      * @param string $valeur
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:      * Linéarise les attributs
484:      *
485:      * @access public
486:      * @return string
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:      * Ecrit l'objet
499:      * 
500:      * @abstract
501:      * @access private
502:      */
503:     abstract public function __toString();
504: }
505: 
506: ?>
Pry API documentation generated by ApiGen 2.8.0