【问题标题】:Yii2 conditional modelYii2条件模型
【发布时间】:2016-07-19 13:59:45
【问题描述】:

我在 yii2 中开发的表单有问题。我已将表单字段设置为整数,并将其连接到另一个模型的属性中。

我希望我可以将其留空,或输入正确的值,但如果我留空值,我会收到错误。

public function rules() {
  return [
    [['id_user'], 'integer', 'message' => ERROR_INTEGER],
    [['id_user'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['id_user' => 'id']],
  ];
}

【问题讨论】:

    标签: yii2


    【解决方案1】:

    尝试使用'skipOnEmpty' => true,

    public function rules() {
        return [
            [['id_user'], 'integer', 'message' => ERROR_INTEGER],
            [['id_user'], 'exist', 
                'skipOnError' => true, 
                'skipOnEmpty' => true,
                'targetClass' => User::className(), 'targetAttribute' => ['id_user' => 'id']
            ],
       ];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      相关资源
      最近更新 更多