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_Element_AutoCompleter
  • Form_Element_Checkbox
  • Form_Element_Colorpicker
  • Form_Element_Date
  • Form_Element_DatePicker
  • Form_Element_Email
  • Form_Element_File
  • Form_Element_Hidden
  • Form_Element_Html
  • Form_Element_Ip
  • Form_Element_Mac
  • Form_Element_Multi
  • Form_Element_NumericStepper
  • Form_Element_Password
  • Form_Element_Radio
  • Form_Element_Select
  • Form_Element_Slider
  • Form_Element_Submit
  • Form_Element_Text
  • Form_Element_Textarea
  • 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:  * Element date. Permet de valider différent type de date dans un champs text
 15:  * @category Pry
 16:  * @package Form
 17:  * @subpackage Form_Element
 18:  * @version 1.1.0 
 19:  * @author Olivier ROGER <oroger.fr>
 20:  */
 21: class Form_Element_Date extends Form_Element_Text
 22: {
 23:     /**
 24:      * Format de date
 25:      *
 26:      * @var string
 27:      * @access protected
 28:      */
 29:     protected $format;
 30:     
 31:     /**
 32:      * Constructeur. Par défaut format fr
 33:      *
 34:      * @param string $nom
 35:      * @param Form_Form $form
 36:      * @access public
 37:      */
 38:     public function __construct($nom,$form)
 39:     {
 40:         parent::__construct($nom,$form);
 41:         $this->format = 'fr';
 42:     }
 43:     
 44:     /**
 45:      * Défini le format de date
 46:      *
 47:      * @param string $format
 48:      * @access public
 49:      * @return Form_Element_Date
 50:      */
 51:     public function format($format)
 52:     {
 53:         $formatAutorise = array('fr','us','sql');
 54:         if(in_array($format,$formatAutorise))
 55:             $this->format = $format;
 56:         else
 57:             throw new InvalidArgumentException('Format : "'.$format.'" de date non reconnu');
 58:         
 59:         return $this;
 60:     }
 61:     
 62:     /**
 63:      * Validation du contenu
 64:      *
 65:      * @param string $value
 66:      * @access public
 67:      * @return boolean
 68:      */
 69:     public function isValid($value)
 70:     {
 71:         if(parent::isValid($value))
 72:         {
 73:             if(!$this->required && empty($value))
 74:                 return true;
 75:             switch($this->format)
 76:             {
 77:                 case 'fr' :
 78:                     if(preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`',$value)>0 || 
 79:                        preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0 ||
 80:                        preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`',$value)>0)
 81:                     {
 82:                         if(strpos($value,':'))
 83:                         {
 84:                             $datetime = explode(' ',$value);
 85:                             list($jour,$mois,$annee) = explode('/',$datetime[0]);
 86:                             $minutes = explode(':',$datetime[1]);
 87:                             $heure  = $minutes[0];
 88:                             $min    =(isset($minutes[1]))?$minutes[1]:'00';
 89:                             $sec    =(isset($minutes[2]))?$minutes[2]:'00';
 90:                             if(is_null($sec)){$sec='00';}
 91:                             if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
 92:                                 return true;
 93:                         }
 94:                         else
 95:                         {
 96:                             list($jour,$mois,$annee)   = explode('/',$value);
 97:                             if(checkdate($mois,$jour,$annee))
 98:                                 return true;
 99:                         }
100:                     }
101:                 case 'us' :
102:                     if(preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`',$value)>0 || 
103:                        preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0||
104:                        preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`',$value)>0)
105:                     {
106:                         if(strpos($value,':'))
107:                         {
108:                             $datetime = explode(' ',$value);
109:                             list($mois,$jour,$annee) = explode('/',$datetime[0]);
110:                             list($heure,$min,$sec)   = explode(':',$datetime[1]);
111:                             if(is_null($sec)){$sec='00';}
112:                             if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
113:                                 return true;
114:                         }
115:                         else
116:                         {
117:                             list($mois,$jour,$annee)   = explode('/',$value);
118:                             if(checkdate($mois,$jour,$annee))
119:                                 return true;
120:                         }
121:                     }
122:                 case 'sql' :
123:                     if(preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`',$value)>0 || 
124:                        preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0||
125:                        preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}$`',$value)>0)
126:                     {
127:                         if(strpos($value,':'))
128:                         {
129:                             $datetime = explode(' ',$value);
130:                             list($annee,$mois,$jour) = explode('/',$datetime[0]);
131:                             list($heure,$min,$sec)   = explode(':',$datetime[1]);
132:                             if(is_null($sec)){$sec='00';}
133:                             if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
134:                                 return true;
135:                         }
136:                         else
137:                         {
138:                             list($annee,$mois,$jour)   = explode('/',$value);
139:                             if(checkdate($mois,$jour,$annee))
140:                                 return true;
141:                         }
142:                     }
143:             
144:                 default : 
145:                     $this->errorMsg = Form_Error::NOTDATE;
146:                     return false;
147:             }
148:             $this->errorMsg = Form_Error::NOTDATE;
149:             return false;
150:         }
151:     }
152:     
153: }
154:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1