Php Yii在两个日期中显示验证错误
在Yii中,我正在编写一个名为Invoice application的小应用程序。我有两个字段,分别是Php Yii在两个日期中显示验证错误,php,yii,validation,Php,Yii,Validation,在Yii中,我正在编写一个名为Invoice application的小应用程序。我有两个字段,分别是发票签发日期和到期日期。我希望对两个输入日期字段进行验证,以便到期日期必须大于发票签发日期。因此,我在模型中制定了以下规则: public function rules (){ array('due_date','compare','compareAttribute'=>'invoice_issue_date', 'operator'=>'>',
发票签发日期
和到期日期
。我希望对两个输入日期字段进行验证,以便到期日期
必须大于发票签发日期
。因此,我在模型中制定了以下规则:
public function rules (){
array('due_date','compare','compareAttribute'=>'invoice_issue_date',
'operator'=>'>',
'allowEmpty'=>false,'message'=>'Due Date must be greater then Invoice Issue Date.'),
}
它工作正常,但当一个字段中有两位数的日期(10到31),而另一个字段中有一位数的日期(1到9),则此验证根本不起作用。有人能告诉我这里怎么了吗?欢迎提供任何帮助和建议
更新
对于日期,我使用CJuiDatePicker来输入日期字段。我认为,这是许多PHP开发人员经常犯的错误
if( '2012-07-23' > '2012-08-17' )
// this is equivalent to comparing two strings , not dates
正确的方法是
if( strtotime('2012-07-23') > strtotime('2012-08-17') )
// I prefer to use "mktime" than "strtotime" for performance reasons
您可能需要编写自己的验证方法,或者在验证之前将这些日期转换为整数
编辑
将其添加到模型类中
public function rules () {
array('due_date', 'isDueDateGreater'),
}
public function isDueDateGreater($attribute, $params) {
if( strtotime($this->due_date) < strtotime($this->invoice_issue_date) )
$this->addError('due_date', 'Due Date must be greater then Invoice Issue Date.');
}
公共功能规则(){
数组('due_date','isduedatemorer'),
}
公共函数isDueDateGreater($attribute,$params){
如果(strotime($this->due\u date)invoice\u issue\u date))
$this->addError('due_date','due date必须大于发票签发日期');
}
我认为,这是许多PHP开发人员经常犯的错误
if( '2012-07-23' > '2012-08-17' )
// this is equivalent to comparing two strings , not dates
正确的方法是
if( strtotime('2012-07-23') > strtotime('2012-08-17') )
// I prefer to use "mktime" than "strtotime" for performance reasons
您可能需要编写自己的验证方法,或者在验证之前将这些日期转换为整数
编辑
将其添加到模型类中
public function rules () {
array('due_date', 'isDueDateGreater'),
}
public function isDueDateGreater($attribute, $params) {
if( strtotime($this->due_date) < strtotime($this->invoice_issue_date) )
$this->addError('due_date', 'Due Date must be greater then Invoice Issue Date.');
}
公共功能规则(){
数组('due_date','isduedatemorer'),
}
公共函数isDueDateGreater($attribute,$params){
如果(strotime($this->due\u date)invoice\u issue\u date))
$this->addError('due_date','due date必须大于发票签发日期');
}
哪个字段有一位数的日期,哪个字段有两位数的日期才能使其不起作用?一位数和两位数是什么意思?这是日期字段的真实值,还是只是日期格式的简写?哪个字段有一位数的日期,哪个字段有两位数的日期,这样就不起作用了?一位数和两位数是什么意思?这是日期字段的真实值,还是只是日期格式的简写?