1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| <?php
#Zend_Loader::loadClass('Zend_Validate');
/**
*
* Validation de formulaires
* à adapter pour utiliser pleinnement les Zend_Validate
*
* @author Jean Yves Terrien
*
*/
class Fast_Validate #extends Zend_Validate
{
protected $_verifFormat = array(
'required' => '.+',
'phone' => '^(\+?[ \t]*[0-9]{2}[ \t]*[0-9]{1}|[ \t]*[0-9]{2})[ \t]*[0-9]{2}[ \t]*[0-9]{2}[ \t]*[0-9]{2}[ \t]*[0-9]{2}$',
'phoneOrNull' => '^(\+?[ \t]*[0-9]{2}[ \t]*[0-9]{1}|[ \t]*[0-9]{2})[ \t]*[0-9]{2}[ \t]*[0-9]{2}[ \t]*[0-9]{2}[ \t]*[0-9]{2}$|^$',
'date' => '^([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})$',
'dateOrNull' => '^([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})$|^$',
'identFT' => '^[a-zA-Z]{4}[0-9]{4}$',
'identFTOrNull' => '^[a-zA-Z]{4}[0-9]{4}$|^$',
'email' => '^[0-9a-zA-Z_\-]+(\\.)?[0-9a-zA-Z_\-]*@([0-9a-zA-Z_-]+\\.)+[a-zA-Z]{2,4}$',
'emailOrNull' => '^[0-9a-zA-Z_\-]+(\\.)?[0-9a-zA-Z_\-]*@([0-9a-zA-Z_-]+\\.)+[a-zA-Z]{2,4}$|^$',
'hour' => '^[0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})?$',
'codePostal' => '^[0-9]{5}$',
'codePostalOrNull' => '^[0-9]{5}$|^$'
);
/**
* Array of validation failure messages
*
* @var array
*/
protected $_messages = array();
/**
* Array of validation failure messages
*
* @var array
*/
protected $_errors = null;
public function __construct($formData, $verif = null)
{
$this->formData = $formData;
if (is_array($verif)) {
$this->_verifForm($verif);
}
}
function _addVerifMessage($key, $msg) {
if (! isset($this->_errors)) $this->_errors = new StdClass();
if (! isset($this->_errors->$key)) $this->_errors->$key = array();
$tab = &$this->_errors->$key;
$tab[] = $msg;
$this->_messages[] = $msg;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns array of validation failure messages
*
* @return array
*/
public function getMessages()
{
return $this->_messages;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns array of validation failure messages
*
* @return array
*/
public function getErrors()
{
return $this->_errors;
}
function _verifForm($verif) {
$this->_errors = null;
#Debug::show('$this->formData',$this->formData);
foreach ($verif as $key=>$value) {
foreach ($value as $ereg=>$msg) {
#Debug::show($key.'=>'.$this->formData->$key, $this->verifFormat[$ereg].'=>'.ereg($this->verifFormat[$ereg], $this->formData->$key));
if (!ereg($this->_verifFormat[$ereg], $this->formData->$key)) {
$this->_addVerifMessage($key, $msg);
}
}
}
return $this->isValid();
}
public function isValid()
{
return ($this->_errors == null);
}
} |
Partager