Prakash
Prakash

Reputation: 109

Add options with same values but different Caption using addMultioption in zend form

I'm creating a select box using zend form element. Now I've a list of values to be displayed in selectbox which may have same values but different captions. Zend forms wont display two options with same value. My code is as follows:

$this->addMultiOption('','-- Select ****** --');
foreach($options as $option) {
    $this->addMultiOption($option->value,$option->caption);
}

Here the values for options can be same like 1,2,3,4,1,2,6,7,8,2,3,2,1... And caption for options are different like aaa,bbb,ccc,ddd,eee,fff

aaa can have value 1, bbb also can have value 1 in this case it would show bbb only and so on.

Is there a way to make this work?

Thanks in advance.

Upvotes: 1

Views: 577

Answers (3)

Wil
Wil

Reputation: 1938

This is my code for changing the values - later I will evaluate only the first part of the string in my ZEND controller or somewhere else. Hope it helps.

$StaffContractTypeData=array();//data options I want in the select

    $options=array();//temp storage array
    $i=0;//iteration counter

    $fk_id_staff_contract_type->addMultiOption("","");//one empty option in my select 

    foreach($StaffContractTypeData as $v) {

        if(in_array($v["code"], $options)){//append the iterator value
            $fk_id_staff_contract_type->addMultiOption($v["code"].$i,$v["label"]);
        }else{
            $fk_id_staff_contract_type->addMultiOption($v["code"],$v["label"]);
            array_push($options, $v["code"]);
        }
        $i++;//change iterator value
    }

Upvotes: 0

Prakash
Prakash

Reputation: 109

As suggested by TDBishop I made minor changes in the code.

$this->addMultiOption('','-- Select ****** --');
foreach($options as $option) {
    $this->addMultiOption($option->value."_".$option->caption,$option->caption);
}

Here, as I had mentioned earlier caption was different all the time so even 'value' being the same, adding caption to suffix would do the work of making each new value different. Now while analyzing data I could parse the values by using explode and utilize both of them. :)

Upvotes: 0

TDBishop
TDBishop

Reputation: 84

You can see the implementation of addMultiOption here. You'll notice that it will cast your option as a string and then force the value on it, overwriting previous values as you have found out.

/**
 * Add an option
 *
 * @param  string $option
 * @param  string $value
 * @return Zend_Form_Element_Multi
 */
public function addMultiOption($option, $value = '')
{
    $option  = (string) $option;
    $this->_getMultiOptions();
    if (!$this->_translateOption($option, $value)) {
        $this->options[$option] = $value;
    }

    return $this;
}

Possibile solutions might be to add a suffix to the options that is a random character string that you cut off when analyzing. You could also create distinct options and map them with a mapping array.

<select>
    <option value="car">Car</option>
    <option value="toy">Toy</option>
<select>

with array

$mapping = array(
    'car' => 'car',
    'toy' => 'car'
);

$trueValue = $mapping[$formOption];

Upvotes: 2

Related Questions