Zend Framework: is there a way to access the element name from a custom validator?

I am writing a custom validator that will validate several other values ​​on a form element. In my form, I call my custom validator like this:

$textFieldOne = new Zend_Form_Element_Text('textFieldOne');
$textFieldOne->setAllowEmpty(false)
             ->addValidator('OnlyOneHasValue', false, array(array('textFieldTwo', 'textFieldThree')));

      

My validator checks if only one of these three fields is relevant (textFieldOne, textFieldTwo, textFieldThree). I want a future developer not to accidentally skip the same field twice.

$textFieldOne->addValidator('OnlyOneHasValue', false, array(array('textFieldOne', 'textFieldTwo', 'textFieldThree')));

      

So far my validator works fine, except when I pass in the same field name as the field on which the validator is set.

In my validator, you can see that I am checking that the value (of the element on which the validator is set). I am also checking the values ​​of other fields that were passed to the validator.

public function isValid($value, $context = null) {

    $this->_setValue($value);
    $this->_context = $context;

    if ($this->valueIsNotEmpty()) {
        if ($this->numberOfFieldsWithAValue() == 0) {
            return true;
        }
        $this->_error(self::MULTIPLE_VALUES);
        return false;
    }

    if ($this->numberOfFieldsWithAValue() == 0) {
        $this->_error(self::ALL_EMPTY);
        return false;
    }

    if ($this->numberOfFieldsWithAValue() == 1) {
        return true;
    }

    if ($this->numberOfFieldsWithAValue() > 1) {
        $this->_error(self::MULTIPLE_VALUES);
        return false;
    }
}

private function valueIsNotEmpty() {
    return Zend_Validate::is($this->_value, 'NotEmpty');
}

private function numberOfFieldsWithAValue() {

    $fieldsWithValue = 0;

    foreach ($this->_fieldsToMatch as $fieldName) {

        if (isset($this->_context[$fieldName]) && Zend_Validate::is($this->_context[$fieldName], 'NotEmpty')) {
            $fieldsWithValue++;
        }
    }
    return $fieldsWithValue;
}

      

My solution is either ...

  • A. Let the developer figure out there is a specific way to do this.
  • B. Ignore $value

    , forcing you to pass all the elements (which are not very different from the first).
  • or C. (if possible) Find the name of the element that first invoked my validator and ignore it from the list $fieldsWithValue

    .

I don't think there is a way to apply a validator on a form without binding it to an element, but it would be even better if it was an option.

How can I solve this problem?

0


source to share


1 answer


Normal, I would advise against such things, but in this case, I believe that a static member in your class will actually provide a good solution to this problem.

With a static member, you can set it to a value the first time you call isValid, and check it on subsequent calls, thereby providing you with a mechanism to do this.



You might want to set this option to use some array in config options so that you can use the namespace and allow multiple validator instances to coexist happily alongside each other for different sets.

The only problem you really have to figure out how to overcome is where you want to display the error, since yes the form itself does not accept validators. if you want all duplicates after the first to display an error, this is not so much a problem.

+1


source







All Articles