user4714953
user4714953

Reputation:

How to add a rule that checks if date given is past 6 days old?

I'm using yii2 basic, i'm trying to add a rule which will compare the date given by the user and give and error if it's older than 6 days.

How would i go about doing this? I'm using datepicker to input the date right now, the format i'm using is: format' => 'd-mm-yyyy

Model.php

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "invoices".
 *
 * @property integer $id
 * @property string $invoice_number
 * @property string $invoice_loadamount
 * @property string $invoice_date
 * @property integer $archive_id
 * @property string $DateProcessed
 *
 * @property Archive $archive
 */
class Invoices extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'invoices';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['invoice_number', 'invoice_loadamount', 'invoice_date'], 'required'],
            [['archive_id', 'invoice_number', 'invoice_loadamount'], 'integer'],
            [['DateProcessed'], 'safe'],            
            [['invoice_number', 'invoice_loadamount', 'invoice_date'], 'string', 'max' => 100],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'invoice_number' => 'Invoice Number',
            'invoice_loadamount' => 'Invoice Loadamount',
            'invoice_date' => 'Invoice Date',
            'archive_id' => 'Archive ID',
            'DateProcessed' => 'Date Processed'];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArchive()
    {
        return $this->hasOne(Archive::className(), ['id' => 'archive_id']);
    }

}

Upvotes: 1

Views: 557

Answers (1)

ttdijkstra
ttdijkstra

Reputation: 638

You can use the core date validator with:

  • min property specifying the lower limit of the accepted date-range.
  • format property to indicate which date-format is used

Add something along these lines:

[['DateProcessed'], 'date', 'format' => "dd-MM-yyyy", 'min' => date("d-m-Y", strtotime('-6 days'))],

Upvotes: 1

Related Questions