yii\widgets\ActiveField::listBox PHP Method

listBox() public method

The selection of the list box is taken from the value of the model attribute.
public listBox ( array $items, array $options = [] )
$items array the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]]. Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.
$options array the tag options in terms of name-value pairs. For the list of available options please refer to the `$options` parameter of [[\yii\helpers\Html::activeListBox()]]. If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly.
    public function listBox($items, $options = [])
    {
        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::activeListBox($this->model, $this->attribute, $items, $options);
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
    public function testListBox()
    {
        $expectedValue = <<<EOD
<input type="hidden" name="DynamicModel[attributeName]" value=""><select id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]" size="4">
<option value="1">Item One</option>
<option value="2">Item 2</option>
</select>
EOD;
        $this->activeField->listBox(["1" => "Item One", "2" => "Item 2"]);
        $this->assertEqualsWithoutLE($expectedValue, $this->activeField->parts['{input}']);
        // https://github.com/yiisoft/yii2/issues/8848
        $expectedValue = <<<EOD
<input type="hidden" name="DynamicModel[attributeName]" value=""><select id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]" size="4">
<option value="value1" disabled>Item One</option>
<option value="value2" label="value 2">Item 2</option>
</select>
EOD;
        $this->activeField->listBox(["value1" => "Item One", "value2" => "Item 2"], ['options' => ['value1' => ['disabled' => true], 'value2' => ['label' => 'value 2']]]);
        $this->assertEqualsWithoutLE($expectedValue, $this->activeField->parts['{input}']);
        $expectedValue = <<<EOD
<input type="hidden" name="DynamicModel[attributeName]" value=""><select id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]" size="4">
<option value="value1" disabled>Item One</option>
<option value="value2" selected label="value 2">Item 2</option>
</select>
EOD;
        $this->activeField->model->{$this->attributeName} = 'value2';
        $this->activeField->listBox(["value1" => "Item One", "value2" => "Item 2"], ['options' => ['value1' => ['disabled' => true], 'value2' => ['label' => 'value 2']]]);
        $this->assertEqualsWithoutLE($expectedValue, $this->activeField->parts['{input}']);
    }
All Usage Examples Of yii\widgets\ActiveField::listBox