2016-05-30 78 views
0

我的问题:我试图在某些字段上验证用户输入。我已经在某些领域创建了规则。提交表单时,它会以json格式显示错误消息并在此停止。yii2在提交时验证表单错误

错误,我得到了提交后的形式 sample error

查看网页:

<?php $form = ActiveForm::begin([ 
      'id'=>'create_company', 
      'method'=>'post', 
      //'enableAjaxValidation' => true, 
      'enableClientValidation' => true, 
     ]); ?> 
<?php echo $form->errorSummary(array($model,$memberPlan)); ?> 
<h4><?php echo Yii::t('app', 'Personal Information'); ?></h4> 
<div class='container-fluid'> 

    <div class='col-sm-4'> 
     <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> 
    </div> 

型号:

public function rules() 
{ 
    return [ 
     [['parent_id', 'gender_id', 'marital_status_id', 'id_type', 'company_id', 'department_id', 'designation_id', 'employment_type_id', 'dependent_type_id', 'cost_centre_id', 'location_id', 'waiting_period', 'relation_id', 'state_id', 'region_id', 'bank_id', 'record_status_id', 'hms_uid', 'status_id', 'created_by', 'modified_by'], 'integer'], 
     [['name', 'company_id', 'department_id', 'designation_id', 'policy_no', 'plan_name','effective_coverage_date'], 'required'], 
     [['dob', 'hired_date', 'confirmation_date', 'first_issue_date', 'effective_coverage_date', 'fullfledge_date', 'reinstatement_date', 'reject_date', 'take_over_date', 'due_date', 'termination_date', 'file_received_date', 'record_status_date', 'created_time', 'modified_time'], 'safe'], 
     [['race', 'grade', 'division', 'employee_no', 'termination_reason', 'member_no', 'client_member_id', 'address1', 'address2', 'address3', 'city', 'postcode', 'account_no', 'payee_name', 'policy_no', 'plan_name', 'product_type', 'plan_code', 'decission_flag', 'card_no', 'vip_remark', 'remarks', 'dba_remarks', 'file_name', 'supervisor_code', 'hr_code'], 'string'], 
     [['salary'], 'number'], 
     [['vip'], 'boolean'], 
     [['name'], 'string', 'max' => 120], 
     [['nationality', 'coverage'], 'string', 'max' => 2], 
     [['id_no', 'alternate_id_no'], 'string', 'max' => 24], 
     [['phone', 'mobile', 'fax'], 'string', 'max' => 20], 
     [['email', 'alternate_email'], 'string', 'max' => 100], 
     ['effective_coverage_date','checkEffectiveCoverageDate', 'on'=>'create'], 
     ['first_issue_date','checkFirstIssueDate', 'on'=>'create'], 
     ['termination_date','checkTerminationDate', 'on'=>'create'], 
     ['email','email'], 
     ['termination_date','checkTerminationDate', 'on'=>'update'], 

    ]; 
} 
public function checkEffectiveCoverageDate($attribute, $params) { 

    if (!empty($this->effective_coverage_date) AND $this->policy_id != '') { 

     if (!$this->withinPolicyStartAndEndDate($this->policy_id, $this->effective_coverage_date)) { 
      $this->addError($attribute, 'Effective Coverage Date cannot be before policy start and end date'); 
     } 
    } 

} 

控制器:

if ($model->load(Yii::$app->request->post()) && $memberPlan->load(Yii::$app->request->post())) { 

    $plan = Plan::find()->where('id = :id',['id'=>$memberPlan->plan_id])->one(); 
    $policy = Policy::find()->where('id = :id',['id'=>$plan->policy_id])->one(); 

    $model->plan_name = $plan->name; 
    $model->policy_no = $policy->policy_no; 
    $model->policy_id = $policy->id; 

    $model->created_by = $userid; 
    $model->created_time = 'now()'; 

    $valid = $model->validate(); 
    $valid = $memberPlan->validate() && $valid; 

    if ($valid) { 

    $transaction = \Yii::$app->db->beginTransaction(); 
    try { 

     $model->setSearchPath($userLogin->getClientCode()); 

     $model->save(); 

     $memberPlan->member_id = $model->id; 
     $memberPlan->plan_id = $memberPlan->plan_id; 
     $memberPlan->start_date = $model->effective_coverage_date; 
     $memberPlan->created_by = $userid; 
     $memberPlan->created_time = 'now()'; 

     $memberPlan->save(); 

     $transaction->commit(); 
     return $this->redirect(['view', 'id' => $model->id]); 

    } catch (Exception $e) { 
     $transaction->rollBack(); 
    } 

    } else { 

    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) && 
    $memberPlan->load(Yii::$app->request->post())) { 

     Yii::$app->response->format = 'json'; 
     return array_merge(ActiveForm::validate($model),ActiveForm::validate($memberPlan)); 

    } else { 

     Yii::$app->response->format = 'json'; 
     $error = ActiveForm::validate($model); 
     return array_merge($error, ActiveForm::validate($memberPlan)); 

    } 

    } 
+0

“* ..它显示JSON格式的错误信息,并停止在那里。* “所以,你实际上想要什么?你没有在问题中描述你在找什么? –

回答

0

您的操作顺序错误。首先你不使用ajax验证,所以你的支票没有意义。

if (Yii::$app->request->isAjax 

这将永远是正确的,因为你不使用ajax。

之后,你正在做的

Yii::$app->response->format = 'json'; 
    $error = ActiveForm::validate($model); 
    return array_merge($error, ActiveForm::validate($memberPlan)); 

你被明确作出响应Ajax和发送回来。它的工作原理就是你告诉它的。

可能要删除

} else { 
     Yii::$app->response->format = 'json'; 
     $error = ActiveForm::validate($model); 
     return array_merge($error, ActiveForm::validate($memberPlan)); 

如果删除此否则无效的模型将被传递给视图(假设你这个代码后呈现视图)和错误将显示每个字段下。

如果你想使AJAX验证,你可以改变控制器代码:

//check ajax validation first 
    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) && $memberPlan->load(Yii::$app->request->post())) { 

      Yii::$app->response->format = 'json'; 
      return array_merge(ActiveForm::validate($model),ActiveForm::validate($memberPlan)); 

     } 

//normal check second  
    if ($model->load(Yii::$app->request->post()) && $memberPlan->load(Yii::$app->request->post())) { 

     $plan = Plan::find()->where('id = :id',['id'=>$memberPlan->plan_id])->one(); 
     $policy = Policy::find()->where('id = :id',['id'=>$plan->policy_id])->one(); 

     $model->plan_name = $plan->name; 
     $model->policy_no = $policy->policy_no; 
     $model->policy_id = $policy->id; 

     $model->created_by = $userid; 
     $model->created_time = 'now()'; 

     $valid = $model->validate(); 
     $valid = $memberPlan->validate() && $valid; 

     if ($valid) { 

     $transaction = \Yii::$app->db->beginTransaction(); 
     try { 

      $model->setSearchPath($userLogin->getClientCode()); 

      $model->save(); 

      $memberPlan->member_id = $model->id; 
      $memberPlan->plan_id = $memberPlan->plan_id; 
      $memberPlan->start_date = $model->effective_coverage_date; 
      $memberPlan->created_by = $userid; 
      $memberPlan->created_time = 'now()'; 

      $memberPlan->save(); 

      $transaction->commit(); 
      return $this->redirect(['view', 'id' => $model->id]); 

     } catch (Exception $e) { 
      $transaction->rollBack(); 
     } 
    } 

的一点是,如果$有效=假时,将再次与它的错误显示页面,因为你已经验证了模型。

1件事,我不认为这会工作

$model->created_time = 'now()'; 

你可能想要做

$model->created_time = new \yii\db\Expression('NOW()'); 
+0

现在工作。我还有另一个问题,我想['termination_date','checkTerminationDate','on'=>'update']规则在更新操作中进行验证。我在动作控制器上添加了$ model-> scenario =“update”,但是当更新表单时,它没有验证termination_date字段 – kasmawati

+0

接受答案并在代码中提出另一个问题。在这个问题中,你没有把这个函数的代码放在那里,所以没有办法检查你的代码。 –