该验证器比对两个特定输入值之间的关系 是否与 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; } }