该验证器比对两个特定输入值之间的关系 是否与 operator 属性所指定的相同。

  • compareAttribute:用于与原属性相比对的属性名称。 当该验证器被用于验证某目标属性时, 该属性会默认为目标属性加后缀_repeat。 举例来说,若目标属性为 password,则该属性默认为 password_repeat
  • compareValue:用于与输入值相比对的常量值。 当该属性与 compareAttribute 属性同时被指定时,该属性优先被使用。
  • operator:比对操作符。默认为 ==,意味着检查输入值是否与 compareAttribute 或 compareValue 的值相等。 该属性支持如下操作符:
    • ==:检查两值是否相等。比对为非严格模式。
    • ===:检查两值是否全等。比对为严格模式。
    • !=:检查两值是否不等。比对为非严格模式。
    • !==:检查两值是否不全等。比对为严格模式。
    • >:检查待测目标值是否大于给定被测值。
    • >=:检查待测目标值是否大于等于给定被测值。
    • <:检查待测目标值是否小于给定被测值。
    • <=:检查待测目标值是否小于等于给定被测值。
  • type: 默认的比对类型是'string',此时将按照字节逐个对比。 当需要比对的值是数字时,需要设置类型$type为 'number',启用数字对比模式。

例如:

//验证合同约定期限必须大于0
            ['sales_contract_agreed_period', 'compare', 'compareValue' => 0, 'operator' => '>', 'message'=>'必须大于0'],
            //验证账龄必须大于0
            ['sales_contract_aging', 'compare', 'compareValue' => 0, 'operator' => '>', 'message'=>'必须大于0'],
            //验证应收款项必须大于0
            ['sales_contract_receivables', 'compare', 'compareValue' => 0, 'operator' => '>', 'message'=>'必须大于0'],

# 增加 type='number'表示当成数值类型比较,否则默认为字符串比较
[['produce_consume_weight'],'compare','compareAttribute'=>'wms_product_in_sheet_in_weight', 'type'=>'number', 'operator'=>'>=','message'=>'消耗量必须大于等于入库重量', 'on'=>self::SC_APPLICATION_FORM],

# 使用compreValue进行比较,但是compareValue只能使用固定的常量值或者使用其他数据模型的值,不能使用此数据模型的某个属性值
[['produce_consume_weight'], 'autoInputProduceConsumeWeight', 'when' => function ($model) {    return ($model->stock_origin_type=="生产加工" && $this->wms_partially_product_in_sheet_in_weight >= $this->getMaxInWeight());}, 'on'=>self::SC_APPLICATION_FORM],

[['produce_consume_weight'], 'validateProduceConsumeWeight', 'when' => function ($model) {    return ($model->stock_origin_type=="生产加工" && $this->wms_partially_product_in_sheet_in_weight < $this->getMaxInWeight());}, 'on'=>self::SC_APPLICATION_FORM],//在特定场景,特定单据类型情况下验证小于等于最大消耗量[['produce_consume_weight'], 'compare', 'compareValue'=>$this->getMaxProduceConsumeWeight(), 'type'=>'number', 'operator'=>'<=','message'=>'消耗量必须小于等于' . \common\models\Base::weightBcdiv($this->getMaxProduceConsumeWeight()), 'when' => function ($model) {    return $model->stock_origin_type=="生产加工" || empty($model->stock_origin_type);}, 'on'=>self::SC_APPLICATION_FORM],
public function autoInputProduceConsumeWeight($attribute, $params)    {        if($this->stock_origin_type=="生产加工" && $this->getScenario()==self::SC_APPLICATION_FORM) {            $this->produce_consume_weight = $this->getMaxProduceConsumeWeight();//            $this->addError('produce_consume_weight', '最后一次入库时必须等于剩余原料消耗量'. \common\models\Base::weightBcdiv($this->produce_consume_weight));        }    }

    public function validateProduceConsumeWeight($attribute, $params)    {        if($this->stock_origin_type=="生产加工" && $this->getScenario()==self::SC_APPLICATION_FORM) {//            if($this->wms_partially_product_in_sheet_in_weight = $this->getMaxInWeight()){//                $this->produce_consume_weight = $this->getMaxProduceConsumeWeight();//            }elseif ($this->produce_consume_weight < $this->wms_partially_product_in_sheet_in_weight) {//                $this->addError('produce_consume_weight', '必须大于等于入库重量');//            }elseif($this->produce_consume_weight < $this->getMaxProduceConsumeWeight()){//                $this->addError('produce_consume_weight', '消耗量必须小于等于' . \common\models\Base::weightBcdiv($this->getMaxProduceConsumeWeight()));//            }else{////            }            if ($this->produce_consume_weight < $this->wms_partially_product_in_sheet_in_weight) {                $this->addError('produce_consume_weight', '必须大于等于入库重量');            }        }    }

    public function getMaxInWeight(){        if($this->stock_origin_type=="生产加工" && $this->getScenario()==self::SC_APPLICATION_FORM){            $produceRecordSheetModel = \core\models\ProduceRecordSheet::findOne(['produce_record_sheet_code'=>$this->produce_record_sheet_number]);            $maxInWeight = bcsub(bcadd($produceRecordSheetModel->produce_record_sheet_product_weight, $produceRecordSheetModel->produce_record_sheet_byproduct_weight, 0) , $produceRecordSheetModel->_getProductInWeight(), 0);            return $maxInWeight;        }else{            return null;        }    }

    /**     * @return string     * 获取最大原料可消耗量     */    public function getMaxProduceConsumeWeight(){        if($this->stock_origin_type=="生产加工" && $this->getScenario()==self::SC_APPLICATION_FORM) {            $produceRecordSheetModel = \core\models\ProduceRecordSheet::findOne(['produce_record_sheet_code' => $this->produce_record_sheet_number]);            $all_consume_weight = \core\components\ArrayHelper::sumObjectColumn(\core\models\WmsPartiallyProductInSheet::find()->where(['or', ['is_del' => 0], ['is_del' => NULL]])->andWhere(['produce_record_sheet_number' => $this->produce_record_sheet_number, 'stock_origin_type' => '生产加工'])->all(), 'produce_consume_weight');            $max_produce_consume_weight = bcsub($produceRecordSheetModel->wms_material_out_sheet_out_weight,                bcadd($all_consume_weight, bcadd($produceRecordSheetModel->produce_record_sheet_retreat_weight, $produceRecordSheetModel->produce_record_sheet_tailing_weight, 0), 0),                0);            return $max_produce_consume_weight;        }else{            return null;        }    }

Yii的数值比较验证器的更多相关文章

  1. yii框架中验证器声明一组内置验证器可以使用短名称引用

    1.内置验证器的短名称分别有: boolean: yii\validators\BooleanValidator captcha: yii\captcha\CaptchaValidator compa ...

  2. yii 验证器和验证码

    http://www.yiiframework.com/doc/api/1.1/CCaptcha http://www.cnblogs.com/analyzer/articles/1673015.ht ...

  3. Thinkphp5 模型 验证器执行顺序问题

    Thinkphp5把模型的验证规则归为一个验证器,这种做法,不知到符不符合大家的心意,反正楼主是比较不爽的 楼主更倾向于tp3.2的验证规则直接写在模型里面,毕竟你的验证规则一般而言是针对模型来验证的 ...

  4. 两步验证杀手锏:Java 接入 Google 身份验证器实战

    两步验证 大家应该对两步验证都熟悉吧?如苹果有自带的两步验证策略,防止用户账号密码被盗而锁定手机进行敲诈,这种例子屡见不鲜,所以苹果都建议大家开启两步验证的. Google 的身份验证器一般也是用于登 ...

  5. PHP验证器类Validator

    Particle\Validator是一个小巧优雅的实用的PHP验证类库,提供了一个非常简洁的API.它无需依赖其他组件,提供友好的文档,并且有利于扩展. 安装 composer require pa ...

  6. 使用google身份验证器实现动态口令验证

    最近有用户反应我们现有的短信+邮件验证,不安全及短信条数限制和邮件收验证码比较慢的问题,希望我们 也能做一个类似银行动态口令的验证方式.经过对可行性的分析及慎重考虑,可以实现一个这样的功能. 怎么实现 ...

  7. 9、 Struts2验证(声明式验证、自定义验证器)

    1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证: ...

  8. linux上使用google身份验证器(简版)

    系统:centos6.6 下载google身份验证包google-authenticator-master(其实只是一个.zip文件,在windwos下解压,然后传进linux) #cd /data/ ...

  9. vue-validator(vue验证器)

    官方文档:http://vuejs.github.io/vue-validator/zh-cn/index.html github项目地址:https://github.com/vuejs/vue-v ...

随机推荐

  1. 【转】iOS-浅谈revoke证书对App的影响

    参考资料:证书-来自苹果官方的介绍 revoke证书对生产.测试环境的影响(1)revoke生产环境证书是不会影响已上架的app. (2)revoke开发环境证书,则安装过该证书的app的用户将无法打 ...

  2. java通过百度AI开发平台提取身份证图片中的文字信息

    废话不多说,直接上代码... IdCardDemo.java package com.wulss.baidubce; import java.io.BufferedReader; import jav ...

  3. python flask里 post请求,JSON数据获取方式总结

    #!flask/bin/python #encodig=utf-8 # _*_ coding:utf-8 _*_ # Writer : byz # dateTime : 2016-08-05 from ...

  4. Consul在.Net Core中初体验

    Consul在.Net Core中初体验 简介 在阅读本文前我想您应该对微服务架构有一个基本的或者模糊的了解 Consul是一个服务管理软件,它其实有很多组件,包括服务发现配置共享键值对存储等 本文主 ...

  5. 阿里云轻量应用服务器debian8.9用apache多端口搭建多站点

    前几天想要再搭个网站玩玩,就用阿里的服务器,apache本地搭建站点和服务器相差不多,然而却踩了大坑,差点耗死在这儿. 先进入apache这个目录,有如下文件夹: 打开ports.conf,  添加 ...

  6. Luogu P3379 【模板】最近公共祖先(LCA)

    qwq 预处理出从$x$节点向上跳2i个节点的序号$p[x][i]$及节点深度$dpth[x]$, 寻找$lca$时,从$Max$(可能的最大深度)到0枚举$i$, 首先把较深的一个节点向上跳至深度相 ...

  7. Vue2.x源码学习笔记-Vue构造函数

    我们知道使用vue.js开发应用时,都是new Vue({}/*options*/) 那Vue构造函数上有哪些静态属性和方法呢?其原型上又有哪些方法呢? 一般我都会在浏览器中输入Vue来look se ...

  8. 震惊!!!python可以用中文来写代码

    python可以用中文来写代码 说明: 偶尔间试了一下,python可以用中文来写代码,除了一些python内置函数,和运算符不能用中文外,其它的比如新定义的类名.函数名.变量名,甚至是函数间传的参数 ...

  9. CentOS 7 源码编译安装 Redis

    1.下载源码并解压 wget http://download.redis.io/releases/redis-4.0.10.tar.gz tar -xzf redis-4.0.10.tar.gz cd ...

  10. DOM(二)

    文档信息 document对象还有一些标准的Document对象所没有的属性: title属性:包含着<title>元素中的文本——显示在浏览器窗口的标题栏或标签页上,通过整个属性可以取得 ...