Yii2 ArrayHelper :: map()显示原始未知的<optgroup>

时间:2017-06-09 09:01:55

标签: php forms yii2

我使用ArrayHelper作为下拉列表,如下所示。但下拉列表显示了&#39; hotel_id&#39;和&#39; room_type&#39;,我不知道从哪里来。有关于此的任何想法吗?

模型文件规则:

    public function rules()
    {
        return [
            [['hotel_id', 'room_type', 'max_persons'], 'required'],
            [['hotel_id', 'max_persons'], 'integer'],
            [['room_type'], 'string', 'max' => 28],
        ];
    }

表单视图文件:

<div class="rooms-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'hotel_id')->dropDownList([
        ArrayHelper::map(Hotels::find()->all(), 'id', 'name')
    ]) ?>

    <?= $form->field($model, 'room_type')->dropDownList([
        ArrayHelper::map(ValidRoomTypes::find()->all(), 'name', 'name')
    ]) ?>

    <?= $form->field($model, 'max_persons')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

Inputting hotel id through its name value in the form    Inputting room type through its name value in the form

1 个答案:

答案 0 :(得分:0)

你应该删除ArrayHelper周围的[]

 <?= $form->field($model, 'hotel_id')->dropDownList(
    ArrayHelper::map(Hotels::find()->all(), 'id', 'name')
) ?>