dongyi1996 2015-03-20 21:10
浏览 361

Yii2:基于两种型号的日期自定义验证

I am having two models namely Patient Admission having two columns such as admission_date and discharge_date and another model daily_ward_entry having a date field say just date both models are related by ipd_patient_id

Now What I want is to create one or two validators, so that I can restrict the date entry in daily_ward_entry between admission_date and discharge_date

I had a look at the compare validator, but I can't make out, how to replace the value to be compared with attribute from another model with relation.

I have tried following variations, but it always throwing error:

Variation one

[$this->discharge_date, 'compare', 'compareValue' => $this->admission_date, 
'operator' => '>='],

error - Unknown Property – yii\base\UnknownPropertyException

Variation two

['discharge_date', 'compare', 'compareValue' => 'admission_date', 'operator' => '>='],

error - Discharge Date must be greater than or equal to "admission_date". This error is generated irrespective of date is lower or greater

variation three

[strtotime($this->discharge_date), 'compare', 'compareValue' =>
strtotime($this->admission_date), 'operator' => '>='],

error - Unknown Property – yii\base\UnknownPropertyException

I need some direction and help. Thanks.

  • 写回答

2条回答 默认 最新

  • dpp34603 2015-03-21 10:43
    关注

    You should be able to achieve your validation rule using a custom validator:

    public function rules()
    {
        return [
            ['daily_ward_entry', 'validateDate'],
        ]
    }
    
    public function validateDate($attribute, $params) {
        if(!($this->patientAdmission->admission_date <= $this->$attribute && $this->$attribute <= $this->patientAdmission->discharge_date)) {
            $this->addError($attribute, 'The Daily Ward Entry must be between the admission date and the discharge date.');
        }
    }
    

    Something like this should conceivably work, I am not entirely sure how your relations are set up so you'll likely have to modify that a little.

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计