如何为DateValidation添加错误消息?

时间:2017-07-03 11:53:02

标签: yii2

在我的模型中,我使用DateValidation

['date_birthday', 'date', 'format' => 'd.m.yy', 'min' => '01.01.1900', 'max' => date('d.m.yy'), 'tooSmall'=>'The date is from past. Try another','tooBig' => 'The date is from future. Try another', 'message' => 'Try to input the date'],

在视图中我调用小部件

 <?php echo $form->field($modelForm, 'date_birthday')->widget(\kartik\date\DatePicker::classname(), [
                        'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
                        'pickerButton' => false,
                        'options' => [
                            'placeholder' => '',
                        ],
                        'pluginOptions' => [
                            'format' => 'dd.mm.yyyy',
                            'autoclose' => true,
                            'showMeridian' => true,
                            'startView' => 2,
                            'minView' => 2,
                        ]
                    ]) ?>

它检查最小和最大日期,但不显示错误消息。我认为这是因为模型和视图中的日期格式不同。如何解决?

1 个答案:

答案 0 :(得分:1)

如果您提交表单,您将看到错误消息。根据此问题,https://github.com/yiisoft/yii2/issues/7745 yii2没有日期的客户端验证

您可以启用ajax验证。在if语句

之前添加创建和更新操作
import org.apache.spark.sql.functions.datediff

val resultDf = df.withColumn("date_diff_days", datediff($"dt1", $"dt2"))

在控制器类的顶部添加use if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } 。在yii\widgets\ActiveForm;文件中为整个表单启用ajax

_form.php

,或仅限该字段

<?php $form = ActiveForm::begin([
    'enableAjaxValidation' => true,
]); ?>

此外,您可以使用startDate和endDate(https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate

添加限制日期的插件选项
<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
...