Creating Fields Comparison Validator for Zend_Form

intro
This article illustrates how to create a custom validator to compare values of elements of Zend_Form. You can download the source code of this article from here.

1.Create Validator
The validator of the following specifications is made.

parameter description
$key String value to identify which fields are compared.
$operator Operator to compare the fields.
The operator is not specified in the element in the comparison destination.
Error Message Key Initial Error Message
fieldNotSame ‘%value%’ is not equal to ‘%key_value%’
fieldNotGreaterThan ‘%value%’ is not greater than ‘%key_value%’
fieldNotGreaterThanEqual ‘%value%’ is not greater than or equal to ‘%key_value%’
fieldNotLessThan ‘%value%’ is not less than ‘%key_value%’
fieldNotLessThanEqual ‘%value%’ is not less than or equal to ‘%key_value%’

Create the file library/My/Validate/FieldCompare.php with the following content.

2.Add Validator
Add the validator like the following content.

This will execute the comparison $email == $email2.
The following is another example.

This will execute the comparison $value >= $min.

7 Comments

  1. Thanks, good one.

    There is only one problem: if I use it for passwords, the %key_value% is being replaced by a visible password text, not *****.

    I added one more operation EQPSW and message MSG_NOT_EQPSW which does not show %key_value%.

  2. In complementary answer to midix, I set a certain echo mode, that replace the %key_value% instead of don’t show it.

    Otherwise, continue your good works.

  3. There could be a problem when it comes to unit testing. So try to reset $_tmp_values while class construction.

    Here is the cleaned constructor:
    °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

    public function __construct($key, $operator = null)
    {
    $this->_key = $key;
    $this->_operator = $operator;
    self::$_key_errors[$key] = array();
    self::$_key_messages[$key] = array();
    self::$_tmp_values = null;
    }

    Now your testing problems should be resolved.

  4. // create class instance counter
    protected static $_instances_created = 0;

    // reset input list when two values were compared
    public static function resetInputList()
    {
    if(self::$_instances_created == 2)
    {
    self::$_tmp_values = null;
    self::$_instances_created = 0;
    return true;
    }

    self::$_instances_created = self::$_instances_created + 1;
    }

    // construct and reset the compare list
    public function __construct($key, $operator = null)
    {
    self::resetInputList();

    $this->_key = $key;
    $this->_operator = $operator;
    self::$_key_errors[$key] = array();
    self::$_key_messages[$key] = array();
    self::$_tmp_operators = null;
    }

  5. Sorry, my first post was wrong. Please refer to my second post where I only reset the list if a second instance of the class was created.

Leave a Reply

Your email address will not be published. Required fields are marked *