0

I have a zend form that has a select box with 1000+ id->name options sorted alphabetically. When rendered and looking at it in a browser, if you type Ch it goes all the way to that option;

Is there a way I can set the value to be selected via the first few letters after the form has been initialized? In other words $form->getElement('name')->setSelected('Ch') or similar;

I know that with setValue(34) I can set the name to be selected that has the ID 34.

1 Answer 1

1

Just wrote the code myself

class My_Form_Element_Select extends Zend_Form_Element_Select{
/**
 * Sets the the first option to start with certain letters to be selected
 * @param string $string The first few letters to search for
 */
public function setSelected($string){
    $string = strtolower($string);
    $options = $this->_getMultiOptions();
    $length = strlen($string);
    foreach($options as $value => $option){ 
        if($string == strtolower(substr($option,0,$length))){
            $this->setValue($value);
            break;  
        }
    }
    return $this;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.