Question

I've got a <select multiple> populated on start up with some values, but where additional options are added via Javascript. However I'd like now to validate this element, that at least one of the options within has been selected.

I'm using Zend_Form_Element_Multiselect to perform the validation:

$tags = new Zend_Form_Element_Multiselect('cms_tags');
$tags->setRegisterInArrayValidator(false)->setRequired(true);

However, of course, this is not working. How can I do something as simple as checking for not emptiness of the cms_tags array without resorting to overloading isValid function?

PS. Validate_Not_Empty is not working as well.

Was it helpful?

Solution

Fortunately, the solution to my question is my code. Unfortunately, Zend, at least to me, has a bug - the problem with my data was an empty value that appeared at the end of the values array, so cms_tags[]=5, cms_tags[]= was considered invalid for this kind of validator. If this a valid behavior, or not, I'll leave it to Zend folks, but this at least answers my issues.

OTHER TIPS

Try something like this:

public function isValid($data) {

    $etat_valid = parent::isValid($data);
    if(isset($data['cms_tags'])){
        // Add your validators
        // for example:
        if("" == $data['cms_tags']){
           $this->getElement('cms_tags')->addErrors(array('This value is required and can not be empty'));
           $etat_valid = false;
        }
    }
    return $etat_valid;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top