【问题标题】:Yii2 ActiveForm unique validation not show error messageYii2 ActiveForm 唯一验证不显示错误信息
【发布时间】:2015-12-16 07:13:54
【问题描述】:

这是我的模特联系人,

public function rules()
{
    return [
        [['fname', 'status','mobile',], 'required'],
        [['user_id', 'contact_type','group_type','passport_no','trip_id','group_id','state',], 'integer'],
        [['fname', 'mname', 'lname', 'status', 'street', 'location', 'post', 'city', 'district','email','job_title','company_name','source'], 'string', 'max' => 500],
        [['department'], 'string', 'max' => 40],
        [['pincode'], 'string', 'max' => 15],
        [['mobile'],'unique'],

此处mobile 所需的验证有效并显示错误消息,但唯一规则有效但不显示任何错误消息

请帮帮我。

【问题讨论】:

  • 你能显示你正在使用的表格吗?

标签: yii2


【解决方案1】:

1) 独特的数据库验证工作。
2) 提交表单后,将检查验证,并且由于唯一验证失败而未保存模型。
3) 如果您想对表单进行唯一验证,请使用 Ajax。

为此,您需要设置'enableAjaxValidation' => true,

$form = ActiveForm::begin([
    'id' => 'contact-form',
     'enableAjaxValidation' => true,
]);

控制器:

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) 
{
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}

Reference

【讨论】:

    【解决方案2】:

    也许你也应该设置targetClass:

    ['email', 'unique', 'targetClass' => '\common\models\User']
    

    【讨论】:

      【解决方案3】:

      我假设您的查看页面为register.php。所以,

      register.php(查看)

      1) 删除 use yii\bootstrap\ActiveForm;(如果存在)

      2) 添加use yii\widgets\ActiveForm;

      3) 在该字段中添加'enableAjaxValidation' => true(您正在验证它的位置。如下所示)

      <?= $form->field($model, 'mobile',['enableAjaxValidation' => true])->textInput(['class'=>'form-control']) ?>
      

      控制器

      见下文(在我写过//Add This Line的地方添加这一行)

      .
      .
      use yii\web\Response; // Add This line
      use yii\widgets\ActiveForm; //Add This Line
      .
      .
      Class SomeClass 
      {
          .
          .
          .   
      
          public function actionRegister() {
      
           $model = new Candidate();
      
           //Add This For Ajax Mobile Exist Validation 
           if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($model);
           } 
      
           else if ($model->load(Yii::$app->request->post())) {
              .
              // Your code
              .
              .
           }
        }
      }
      

      型号

      [['mobile'],'unique','message'=>'Mobile No Already Exist'],
      

      供参考 - 点击Validate unique value from database in Yii2

      【讨论】:

        【解决方案4】:
        if ($model->validate()) {
            //SAVE YOUR DATA
        } else {
            // HERE YOU CAN PRINT THE ERRORS OF MODEL
            $data = $model->getErrors();
            Yii::$app->session->setFlash('message', $data['password'][0]);// its dislplays error msg on your form
        }
        

        【讨论】:

          【解决方案5】:

          我的模型中的这条规则对我有用:

          ['email', 'required'],
          ['email', 'email'],
          ['email', 'unique', 'targetClass' => '\app\models\Student', 'message' => 'This email address has already been taken.'],
          

          【讨论】:

          • 请正确格式化代码。使用 4 个空格缩进或 `` 引号
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-03
          • 2018-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多