Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 3.93 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Zend Framework validation and/or filtering issue
  2. <?php
  3.  
  4. class Test_Form_Check extends Zend_Form
  5. {
  6.  
  7.     public function init()
  8.     {
  9.  
  10.         $mobile= new Zend_Form_Element_Text( 'mobile' );
  11.         $mobile->addFilter(new Test_Filter_MobileFilter('44'))
  12.                 ->addValidator(new Test_Validate_Mobile())
  13.                 ->setRequired( true );
  14.  
  15.         $mobile1= new Zend_Form_Element_Text( 'mobile_confirm' );
  16.         $mobile1->addFilter(new Test_Filter_MobileFilter('44'))
  17.                 ->addValidator(new Test_Validate_Mobile())
  18.                 ->addValidator('Identical', false, array('token' => 'mobile', 'messages' => 'Mobile numbers do not match.'))
  19.                 ->setRequired( true );
  20.  
  21.         $Add = new Zend_Form_Element_Submit('Add');
  22.         $Add->setLabel('Submit');
  23.  
  24.         $this->addElement($mobile)
  25.                 ->addElement($mobile1)
  26.                 ->addElement( $Add );
  27.  
  28.     }
  29. }
  30.  
  31.  
  32. class Test_Validate_Mobile extends Zend_Validate_Abstract
  33. {
  34.     const NOT_DIGITS   = 'notDigits';
  35.     const STRING_EMPTY = 'digitsStringEmpty';
  36.     const INVALID      = 'digitsInvalid';
  37.     const INVALIDPHONE = 'phonenumberinvalid';
  38.  
  39.     protected static $_filter = null;
  40.  
  41.     protected $_messageTemplates = array(
  42.         self::NOT_DIGITS   => "'%value%' must contain only digits",
  43.         self::STRING_EMPTY => "'%value%' is an empty string",
  44.         self::INVALID      => "Invalid type given. String, integer or float expected",
  45.         self::INVALIDPHONE => "Invalid number, try with country code",
  46.     );
  47.  
  48.  
  49.     public function isValid($value)
  50.     {
  51.         if (!is_string($value) && !is_int($value) && !is_float($value)) {
  52.             $this->_error(self::INVALID);
  53.             return false;
  54.         }
  55.  
  56.         if (!preg_match('/^(447)[0-9]{9}$/', $value)) {
  57.             $this->_error(self::INVALIDPHONE);
  58.             return false;
  59.         }
  60.  
  61.         $this->_setValue((string) $value);
  62.  
  63.         if ('' === $this->_value) {
  64.             $this->_error(self::STRING_EMPTY);
  65.             return false;
  66.         }
  67.  
  68.         if (null === self::$_filter) {
  69.             require_once 'Zend/Filter/Digits.php';
  70.             self::$_filter = new Zend_Filter_Digits();
  71.         }
  72.  
  73.         if ($this->_value !== self::$_filter->filter($this->_value)) {
  74.             $this->_error(self::NOT_DIGITS);
  75.             return false;
  76.         }
  77.  
  78.         return true;
  79.     }
  80. }
  81.  
  82.  
  83. class Test_Filter_MobileFilter implements Zend_Filter_Interface
  84. {
  85.     /**
  86.     * Default country code
  87.     *
  88.     * @var string
  89.     */
  90.     protected $_def_country = '44';
  91.  
  92.     public function __construct($options = null)
  93.     {
  94.         if ($options !== null) {
  95.             if (is_string($options) && is_numeric($options)) {
  96.                 $this->_def_country = $options;
  97.             }
  98.             else {
  99.                 require_once 'Zend/Filter/Exception.php';
  100.                 throw new Zend_Filter_Exception('Options not valid country code');
  101.             }
  102.         }
  103.     }
  104.  
  105.     public function filter($value)
  106.     {
  107.         if (!empty($value)) {
  108.             $replace = array(' ','+','-','(',')');
  109.  
  110.             $value = str_replace($replace, '', $value);
  111.             $value = preg_replace('/A0/', $this->_def_country, $value);
  112.  
  113.         }
  114.         return $value;
  115.     }
  116.  
  117. }
  118.        
  119. class My_Validate_Identical extends Zend_Validate_Identical
  120. {
  121.     /**
  122.      *
  123.      * @var Zend_Form_Element
  124.      */
  125.     protected $_element = null;
  126.  
  127.     public function __construct($token = null)
  128.     {
  129.         if (is_array($token) && array_key_exists('element', $token)) {
  130.             $this->_element = $token['element'];
  131.         }
  132.         parent::__construct($token);
  133.     }
  134.     public function isValid($value, $context = null)
  135.     {
  136.         $context[$this->getToken()] = $this->_element->getValue();
  137.         return parent::isValid($value, $context);
  138.     }    
  139. }
  140.        
  141. $mobile1->addValidator(new My_Validate_Identical(array(
  142.                 'token' => 'mobile',
  143.                 'messages' => 'Mobile numbers do not match.',
  144.                 'element' => $mobile
  145.                 )))