php Yii Framework 2.0 规则日期验证器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26088435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii Framework 2.0 Rules Date Validator
提问by O Connor
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\Validatorand known all the validator keys which can be used inside of the rules()method in a model class. When I use the datekey 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.
我正在使用 Yii 框架 2.0。我有一个带有文本输入字段的表单,用于日期。我已经阅读了关于 yii \validators\Validator 类的 Yii Framework 2.0,并且知道所有可以在模型类中的rules()方法内部使用的验证器键。当我使用如下日期键时,它不会验证任何内容。这意味着我仍然可以在该输入字段中放入一些文本并可以发布表单。
When I changed it into booleanor 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?
当我将其更改为boolean或email 时,我可以看到当我在输入字段中输入错误时,它的验证效果非常好。如何使用 Yii Framework 2.0 验证输入字段内的日期值?
My rules() method:
我的规则()方法:
public function rules()
{
return [
[['inputfield_date'], 'required'],
[['inputfield_date'], 'safe'],
[['inputfield_date'], 'date'],
];
}
My view page:
我的查看页面:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'inputfield_date')->textInput(); ?>
<?php ActiveForm::end(); ?>
回答by Chloe
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.
男孩,Yii 文档很烂。他们甚至不举一个例子。根据 O'Connor 的回答,这对我有用,因为我以2015-09-11
格式分配值。
// Rule
[['event_date'], 'date', 'format' => 'php:Y-m-d']
// Assignment
$agkn->event_date = date('Y-m-d');
The docsdon'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!
该文档甚至不指定format
或timestampAttribute
从,或如何使用它们来了。它甚至没有说什么的挫折感from_date
和to_date
是。最重要的是,没有例子!
回答by O Connor
Working solution. My rules() method:
工作解决方案。我的规则()方法:
public function rules()
{
return [
[['inputfield_date'], 'required'],
[['inputfield_date'], 'safe'],
['inputfield_date', 'date', 'format' => 'yyyy-M-d H:m:s'],
];
}
My form in the view page:
我在视图页面中的表单:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'inputfield_date')->textInput(); ?>
<?php ActiveForm::end(); ?>
My method in controller:
我在控制器中的方法:
if ($model->load(Yii::$app->request->post()) && $model->validate()):
if($model->save()):
// some other code here.....
endif;
endif;
Note that the date format depends on how you define your date format input field. Note once again that this is not an AJAX validator. After clicking on the submit button, you will see the error message if you enter something else which is not a date.
请注意,日期格式取决于您如何定义日期格式输入字段。再次注意,这不是 AJAX 验证器。单击提交按钮后,如果您输入其他不是日期的内容,您将看到错误消息。
回答by Azraar Azward
You can validate for date like this from model rules
您可以从模型规则中验证这样的日期
public function rules(){
return [
[['date_var'],'date', 'format'=>'d-m-yy'],
[['from_date', 'to_date'], 'default', 'value' => null],
[['from_date', 'to_date'], 'date'],
];
}
回答by webeno
Not 100% sure how it is in Yii2, but going out from Yii1 that had to look like this...
不是 100% 确定它在 Yii2 中的情况,但是从 Yii1 出去必须看起来像这样......
array('org_datetime', 'date', 'format'=>'yyyy-M-d H:m:s'),
(source: http://www.yiiframework.com/wiki/56/#hh8)
(来源:http: //www.yiiframework.com/wiki/56/#hh8)
...I'd say i'd have to look something like this in Yii2:
...我会说我必须在 Yii2 中看起来像这样:
['shopping_date', 'date', 'format' => 'yyyy-M-d H:m:s'],