Search code examples
phpzend-frameworkzend-formzend-validate

case insensitive inArray for Zend Form elements like Zend_Form_Element_MultiCheckbox and so on


By default, some form elements (such as Zend_Form_Element_MultiCheckbox and so on) register an InArray validator which validates against the array keys of registered options. This validator used case sensitive compaision. What is the simplest what to make case insensitive? The only solution that occurs to me now looks something like this. Turn off this validator:

$element->setRegisterInArrayValidator(false);

Create new validator that will make case insensitive comparision and add this validation for the element. Is this the only way to solve this problem? It seems it would be nice if there is a standard way to make inArray validator to make case insensitive comparision.


Solution

  • Zend_Validate_InArray uses in_array function, and the documentation for the function says:

    If needle is a string, the comparison is done in a case-sensitive manner.

    Zend_Validate_InArray includes option for strict comparison, which is in turn handled by in_array function, too. But there is no option for case-insensitive comparison.

    With resursive option, Zend_Validate_InArray makes use of recursive iterators instead of in_array and compares the values by means of == (or, in strict mode, ===) operator. Which is case-sensitive, too.

    So there is no other option but to create your own validator (which I would inherit from Zend_Validate_InArray, overriding public function isValid($value)).