Posts Tagged Zend_Input_Filter

Zend_Validate_StringEquals

If you ever wonder where that 'StringEquals' validator rule taken as example from the Zend_Filter_Input documentation page results in an error like below, well read again. It was clearly stated as 'hypothetical'.

Plugin by name 'StringEquals' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/

Given such validator would be useful on a number of situations i.e. confirming passwords, emails, etc. I present to you my own version of the class.

  1.  
  2. <?php
  3. class Zend_Validate_StringEquals extends Zend_Validate_Abstract
  4. {
  5. const NOT_EQUAL = 'stringNotEqual';
  6. const MISSING = 'stringMissing';
  7.  
  8. /**
  9.   * @var array
  10.   */
  11. protected $_messageTemplates = array(
  12. self::NOT_EQUAL => "%field1% and %field2% are not equal.",
  13. self::MISSING => "One or both strings are missing."
  14. );
  15.  
  16. /**
  17.   * @var array
  18.   */
  19. protected $_messageVariables = array(
  20. 'field1' => '_field1',
  21. 'field2' => '_field2'
  22. );
  23.  
  24. protected $_case = false;
  25. protected $_field1 = null;
  26. protected $_field2 = null;
  27.  
  28. /**
  29.   * Sets validator options
  30.   *
  31.   * @param boolean $case
  32.   * @return void
  33.   */
  34. public function __construct($case = false)
  35. {
  36. $this->_case = $case;
  37. }
  38.  
  39. /**
  40.   * Defined by Zend_Validate_Interface
  41.   *
  42.   * Returns true if and only if the the 2 strings are equal
  43.   *
  44.   * @param array $value
  45.   * @return boolean
  46.   */
  47. public function isValid($value)
  48. {
  49. if(!is_array($value) OR sizeof($value) < 2) {
  50. $this->_error(self::MISSING);
  51. }
  52.  
  53. $this->_field1 = array_shift($value);
  54. $this->_field2 = array_shift($value);
  55.  
  56. if($this->_case === true) $function = 'strcmp';
  57. else $function = 'strcasecmp';
  58.  
  59. if(0 !== $function($this->_field1,$this->_field2)) $this->_error(self::NOT_EQUAL);
  60.  
  61. if (count($this->_messages)) {
  62. return false;
  63. } else {
  64. return true;
  65. }
  66. }
  67. }
  68.  

Here is a sample test case. Validate password and confirm password elements represented by 'password' and 'cpassword' element names respectively.

  1.  
  2. $filters = array('password' => 'StringTrim', 'cpassword' => 'StringTrim');
  3. $validators = array(
  4. 'Password' => array(
  5. 'presence' => 'required',
  6. array('StringLength',5,15),
  7. 'fields' => 'password',
  8. 'messages' => "Passwords must be between 5 and 15 characters in length."),
  9. 'Confirm password' => array(
  10. array('StringEquals'),
  11. 'fields' => array('password','cpassword'),
  12. 'messages' => array(
  13. 0 => array(
  14. Zend_Validate_StringEquals::NOT_EQUAL => "Passwords does not match.",
  15. Zend_Validate_StringEquals::MISSING => "Both password fields must be filled."))));
  16.  
  17. $inputdata = new Zend_Filter_Input($filter,$validators,$_POST,$options);
  18.  

, , ,

No Comments