yii之validate常用规则集合

['home_team_id', 'compare', 'compareAttribute' => 'away_team_id', 'operator' => '!=', 'message' => 'Please choose a different teams'],

[['sample_table_item_id_2'], function ($attr) {
                $name = $this->getAttributeLabel($attr);
                if (!GSampleTableItem::findOne([
                    'uuid' => (string)$this->$attr,
                    'status' => [GSampleTableItem::STATUS_1],
                    'data_type' =>[GSampleTableItem::DATA_TYPE_2]
                ])) {
                    $this->addError($attr, "{$name}不是数值类型");
                }
            }, 'skipOnEmpty' => false, 'skipOnError' => false],
//范围
['fixed_2_type', 'in', 'range' => array_keys(self::getType()),'message'=>'type类型不合法!'],
// 检查 "password" 属性的值是否与 "password_repeat" 的值相同
['password', 'compare'],
// 和上一个相同,只是明确指定了需要对比的属性字段
['password', 'compare', 'compareAttribute' => 'password_repeat'],
// 检查年龄是否大于等于 30
['age', 'compare', 'compareValue' => 30, 'operator' => '>='],
//比较日期
​['fromDate', 'date', 'timestampAttribute' => 'fromDate'],
['toDate', 'date', 'timestampAttribute' => 'toDate'],
['fromDate', 'compare', 'compareAttribute' => 'toDate', 'operator' => '<', 'enableClientValidation' => false],

// 若 "age" 为空,则将其设为 null
['age', 'default', 'value' => null],
// 若 "country" 为空,则将其设为 "USA"
['country', 'default', 'value' => 'USA'],
// 若 "from" 和 "to" 为空,则分别给他们分配自今天起,3 天后和 6 天后的日期。
[['from', 'to'], 'default', 'value' => function ($model, $attribute) {
        return date('Y-m-d', strtotime($attribute === 'to' ? '+3 days' :'+6 days'));
    }],
//file
// 检查 "primaryImage" 是否为 PNG, JPG 或 GIF 格式的上传图片。
// 文件大小必须小于  1MB
['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024],

//filter
// trim 掉 "username" 和 "email" 输入
[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
// 标准化 "phone" 输入
['phone', 'filter', 'filter' => function ($value) {
    // 在此处标准化输入的电话号码
    return $value;
}],
// 标准化 "phone" 使用方法 "normalizePhone"
['phone', 'filter', 'filter' => [$this, 'normalizePhone']],
    public function normalizePhone($value) {
        return $value;
    }],
// trim 掉 "username" 和 "email" 两侧的多余空格
[['username', 'email'], 'trim'],

//场景
[['create_users_id'], 'required', 'on' => 'create'],
$model = new GTask;
$model->scenario = 'create';
1. 加载请求参数到model:
$model->load(\Yii::$app->request->post(),'');
2.判读请求参数合法性:
if(!$model->validate()){
    return RestStatus::buildRestErrRes(401, $model->getErrors());
}
3. 寄存请求参数,执行对应的db逻辑:CRUD
4. 查找接口已list返回,其余增、改、删接口均需要做捕获异常处理
5.load()与validate()要一起用
      load()只是加载数据,validate()才是验证。
6.更新和添加都用save()
   1.1:可以不用validate(),因为save方法会先调用validate()再执行insert()或者update()。
   1.2:yii通过$model->isNewRecord 来判断是不是一条新纪录,然后调用insert()或者update()。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值