Yii Framework 2.0 规则日期验证器

2022-01-04 00:00:00 validation date php yii2 yii

我使用的是 Yii 框架 2.0.我有一个带有文本输入字段的表单,用于日期.我已经阅读了关于 Class yiivalidatorsValidator 的 Yii Framework 2.0 并且知道可以在模型类中的 rules() 方法内使用的所有验证器键.当我使用下面的 date 键时,它不会验证任何内容.这意味着我仍然可以在该输入字段中输入一些文本并可以发布表单.

I am using Yii Framework 2.0. I have a form with a text input field which is meant for a date. I have read the Yii Framework 2.0 about the Class yiivalidatorsValidator and known all the validator keys which can be used inside of the rules() method in a model class. When I use the date key as below, it does not validate anything. It means that I still can put some text in that input field and can post the form.

当我将其更改为 boolean 或 email 时,我可以看到当我在输入字段中输入错误时,它可以很好地验证.如何使用 Yii Framework 2.0 验证输入字段内的日期值?

When I changed it into boolean or email, I could see that it validates very well when I put something wrong in the input field. How can I validate a date value inside of an input field with Yii Framework 2.0?

我的 rules() 方法:

My rules() method:

public function rules()
{
    return [
         [['inputfield_date'], 'required'],
         [['inputfield_date'], 'safe'],
         [['inputfield_date'], 'date'],
    ];
}

我的查看页面:

<?php $form = ActiveForm::begin(); ?>
     <?= $form->field($model, 'inputfield_date')->textInput(); ?>
<?php ActiveForm::end(); ?>

推荐答案

Yii 文档太烂了.他们甚至不举一个例子.根据奥康纳的回答,这对我有用,因为我以 2015-09-11 格式分配值.

Boy the Yii docs suck. They don't even give an example. Working from O'Connor's answer, this worked for me since I was assigning the value in 2015-09-11 format.

// Rule
[['event_date'], 'date', 'format' => 'php:Y-m-d']
// Assignment
$agkn->event_date = date('Y-m-d');

docs 甚至没有指定 formattimestampAttribute 来自,或如何使用它们.它甚至没有说明 from_dateto_date 到底是什么.最重要的是,没有示例!

The docs don't even specify where format or timestampAttribute came from, or how to use them. It doesn't even say what the heck from_date and to_date are. And most of all, no examples!

相关文章