2

Yiiでは、請求書アプリケーションと呼ばれる小さなアプリケーションを書いています。その中で、とという2つのフィールドがInvoice Issue DateありDue Dateます。両方の入力日付フィールドの検証がDue Date必要なので、。より大きくする必要がありますInvoice Issue Date。そこで、モデルで次のルールを作成しました。

public function rules (){
    array('due_date','compare','compareAttribute'=>'invoice_issue_date',
          'operator'=>'>',
          'allowEmpty'=>false,'message'=>'Due Date must be greater then Invoice Issue Date.'),
}

正常に機能していますが、1つのフィールドに2桁の日付(10から31)があり、別のフィールドに1桁の日付(1から9)がある場合、この検証はまったく機能しません。誰かがここで何が悪いのか教えてもらえますか?ヘルプや提案は大歓迎です。

アップデート

CJuiDatePicker日付フィールドの入力に使用している日付の場合。

4

1 に答える 1

0

これは多くの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.');
}
于 2012-09-26T07:40:31.827 に答える