create a validation to limit one record within one day for a specific item

I have a form which takes various charges on daily basis for patients in hospital. I want to limit one entry for any patient for a specific charge(service charge) in one day.

How can I create a validation like this.

I am looking for some direction to create such a rule.


The best way I can think of is like this:

Assume that the name of the active record class is StudentLog.

public function rules()


     return [





public function validateOnlyOne($attribute, $params)


      $st = StudentLog::find()->where(['log_date' => $this->log_date])->one();


           $this->adError('Can only have one record per day');


Hi Hodges thanks

my date field is date_time field, how can I convert it to date only format to adapt in ur suggested query.

In that case I think what you want to do is change your query so its like this:

SELECT * FROM StudentLog WHERE DATE(log_date) = ‘2010-04-29’

or in Yii2 format it would look like this:

$st = StudentLog::find()->where([DATE('log_date') => $this->log_date])->one();


           $this->adError('Can only have one record per day');