【问题标题】:Yii2 not showing error message on dropdownListYii2 未在下拉列表中显示错误消息
【发布时间】:2018-09-08 03:12:34
【问题描述】:

我为我的项目创建了一个 dropdownList,并按照我的模型规则中的要求创建了该字段。

考虑中

use app\models\Constituency;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select



<?php
  $constituency=Constituency::find()->all();
  $listData=ArrayHelper::map($constituency,'constituency','constituency');

?>
<?php
   echo '<label class="control-label">Constituency</label>';
   echo Select2::widget([
   'model' => $model,
   'attribute' => 'place',
   'data' => $listData,
   'options' => ['placeholder' => 'Select a constituency'],
   'pluginOptions' => [
   'allowClear' => true
   ],
  ]);
?>  

在模型中

['place', 'required','message'=>'Place is required'],  

即使我厌倦了为该字段提供自定义错误消息,但我尝试的一切都失败了。
我在此处附上页面的屏幕截图。
如您所见,它显示没有必需的错误。

谁能告诉我我错过了什么??

【问题讨论】:

  • 你是如何渲染“手机号”字段的?
  • = $form->field($model, 'phone')->textInput(['min' => 10,'type'=>'number']) ?>跨度>

标签: php validation drop-down-menu yii2


【解决方案1】:

使用活动字段小部件:

<?= $form->field($model, 'place')->widget(Select2::className(), [
    'data' => $listData,
    'options' => ['placeholder' => 'Select a constituency'],
    'pluginOptions' => ['allowClear' => true],
]) ?>

【讨论】:

  • 谢谢,你能说说为什么它在我的方法中不起作用
  • 您缺少其他容器来存放由活动字段小部件自动完成的错误消息 - 这些容器是客户端验证正确显示错误所必需的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2016-03-18
  • 2014-06-19
  • 2013-09-27
  • 2019-02-03
  • 1970-01-01
  • 2016-09-25
相关资源
最近更新 更多