'商品ID不能为空'], [['name'], 'required', 'message' => '请填写商品名称'], [['categoryIdList'], 'required', 'message' => '请选择商品分类'], // 数据类型验证 [['id', 'status', 'delStatus', 'setItemNum'], 'integer'], [['name', 'cover'], 'string'], [['price'], 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'], [['sold'], 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'], ['flowerNum', 'integer', 'min' => 0, 'message' => '花朵数量必须大于或等于0'], ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'], [['autoRise'], 'in', 'range' => [0, 1]], [['stockSet'], 'in', 'range' => [0, 1]], [['stock'], 'integer', 'min' => 0, 'message' => '库存不能小于0'], [['priceType'], 'in', 'range' => [0, 1]], [['delStatus'], 'in', 'range' => [0, 1]], [['needSend'], 'in', 'range' => [0, 1]], [['freightType'], 'in', 'range' => [0, 1]], [['status'], 'in', 'range' => [0, 1]], // 数组类型验证 [['shopImg'], 'validateShopImg'], [['categoryIdList'], 'validateCategoryIdList'], // 默认值设置 ['flowerNum', 'default', 'value' => 0], ['weight', 'default', 'value' => 1],//当没有重量时,默认给1千克 ['setItemNum', 'default', 'value' => 0],// 在更新时,没传值,会为 NULL 而导致报错 ['status', 'default', 'value' => 1], ['delStatus', 'default', 'value' => 0], ['categoryIdList', 'default', 'value' => []], ['stock', 'default', 'value' => 0], ]; } /** * 验证商品图片 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateShopImg($attribute, $params) { if (!empty($this->$attribute) && !is_array($this->$attribute)) { $this->addError($attribute, '商品图片格式不正确'); } } /** * 验证分类ID列表 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateCategoryIdList($attribute, $params) { if (!empty($this->$attribute) && !is_array($this->$attribute)) { $this->addError($attribute, '商品分类格式不正确'); } } /** * 获取属性标签 * @return array */ public function attributeLabels() { return [ 'id' => '商品ID', 'name' => '商品名称', 'price' => '商品价格', 'priceType' => '价格类型', 'flowerNum' => '花朵数量', 'shopImg' => '商品图片', 'cover' => '商品封面图', 'stock' => '商品库存', 'categoryIdList' => '商品分类', 'status' => '商品状态', 'delStatus' => '删除状态', 'setItemNum' => '商品子项数量', 'itemList' => '商品子项列表', ]; } }