0规则日期验证器

0规则日期验证器

本文介绍了Yii Framework 2.0规则日期验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Yii Framework 2.0。我有一个带有文本输入字段的表单,用于日期。我已经阅读了有关 Class yii\validators\Validator 的Yii Framework 2.0,并且知道了所有可以在 rules()方法中使用的验证器密钥。模型类。当我按如下所示使用日期键时,它不会验证任何内容。这意味着我仍然可以在该输入字段中输入一些文本并可以发布该表单。

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 yii\validators\Validator 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 电子邮件,当我在输入字段中输入错误内容时,我可以看到它的验证效果很好。如何使用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(); ?>


推荐答案

Boy the Yii docs suck。他们甚至没有举一个例子。使用O'Connor的答案,这对我有用,因为我以 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');

甚至都没有指定格式 timestampAttribute 来自或如何使用它们。甚至都没有说 from_date to_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!

这篇关于Yii Framework 2.0规则日期验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:37