我正在处理一个 yii2 页面,它在向 Select2 小部件添加占位符或提示时遇到问题。这是我的代码:
<?php
use yiihelpersHtml;
use kartikwidgetsActiveForm;
use kartikbuilderForm;
use kartikdatecontrolDateControl;
use yiihelpersArrayHelper;
/**
* @var yiiwebView $this
* @var appmodelsFinancialAccounts $model
* @var yiiwidgetsActiveForm $form
*/
?>
<div class="financial-accounts-form">
<?php $form = ActiveForm::begin(['type' => ActiveForm::TYPE_VERTICAL]); echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 1,
'attributes' => [
'type_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'kartikwidgetsSelect2', 'options' => ['data'=>ArrayHelper::map(appmodelsFinancialAccountType::find()->all(), 'type_id', 'name')]],
'account_name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Name...', 'maxlength' => 100]],
'account_code' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Code...', 'maxlength' => 10]],
'parent_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'kartikwidgetsSelect2', 'options' => ['data'=>ArrayHelper::map(appmodelsFinancialAccounts::find()->all(), 'account_id', 'account_name'), 'placeholder' => 'Select a Parent Account...']],
'description' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Description...', 'maxlength' => 250]],
],
]);
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
);
ActiveForm::end(); ?>
</div>
问题出在 parent_id 属性中,因为我无法像大多数教程建议的那样添加占位符作为选项。每次我尝试这样做时,我都会收到这样的错误:
Unknown Property – yiibaseUnknownPropertyException
Setting unknown property: kartikwidgetsSelect2::placeholder
有谁知道我该如何解决这个问题?我的主要问题是,在提交数据时,我不能将此选项留空,但这是可能性之一。它迫使我提交选定的项目。
您会注意到,如果您仔细遵循文档中的示例,占位符需要包装在选项数组中。
'parent_id' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => 'kartikwidgetsSelect2',
'options' => [
'data' => ArrayHelper::map(appmodelsFinancialAccounts::find()
->all(), 'account_id', 'account_name'),
'options' => ['placeholder' => '...'],
'pluginOptions' => ['allowClear' => true],
]
],