2016-10-01 4 views
0

사용자가 주문을 만들고 검토 명령을 보내고 워크 플로의 여러 단계를 수행 할 수있는 Yii 2.0 응용 프로그램을 개발 중입니다.Yii2 유효성 확인 컨트롤러 동작

어제까지 고객이 주문 검토를위한 주문을 보내기 전에 초안으로 간주 될 가능성이 있음을 알 수 있습니다. 즉, 작성시 유효성 검사를 사용 중지하고 사용자가 검토 보내기 버튼을 클릭 할 때 유효성을 검사해야한다는 의미입니다. 나는 Yii 2.0이 시나리오를 지원하지만 Read to Review 버튼이 읽기 전용보기로 표시되기 때문에 시나리오가이 시나리오에 적용되지 않는다고 알고 있습니다. 이렇게하면 send_to_review 뷰가 없기 때문에 컨트롤러 동작 내부에서 유효성 검사를 수행해야합니다. 어떻게이 작업을 수행 할 수 있습니까 (컨트롤러 동작 내부의 모델 유효성 검사를 의미합니까)? 여기

는 내가 해결하기 위해 필요하면 TODO 인 컨트롤러 액션 코드

public function actionSendToReview($id) 
{ 
    if (Yii::$app->user->can('Salesperson')) 
    { 
     $model = $this->findModel($id); 
     if ($model->orden_stage_id == 1 && $model->sales_person_id == Yii::$app->user->identity->id) 
     { 
      $model->orden_stage_id = 2; 
      $model->date_modified = date('Y-m-d h:m:s'); 
      $model->modified_by = Yii::$app->user->identity->username; 

      //TODO: Validation logic if is not valid show validation errors 
      //for example "For sending to review this values are required: 
      //list of attributes in bullets" 
      //A preferred way would be to auto redirect to update action but 
      //showing the validation error and setting scenario to    
      //"send_to_review". 


      $model->save(); 
      $this::insertStageHistory($model->order_id, 2); 
      return $this->redirect(['index']); 
     } 
     else 
     { 
      throw new ForbiddenHttpException(); 
     } 
    } 
    else 
    { 
     throw new ForbiddenHttpException(); 
    } 
} 

입니다. 옵션 1 : 동일한보기에 유효성 검사 오류가 표시되고 사용자가 클릭해야 함 업데이트 단추가 요청한 값을 변경 한 다음 저장하고 검토 보냄을 다시 시도하십시오. 옵션 2 : 컨트롤러에서 발견 된 시나리오 및 유효성 검사 오류를 이미 업데이트보기로 자동으로 리디렉션합니다.

감사합니다,

안부

답변

0

당신은 컨트롤러에서 유효성 검사에 $model ->validate()를 사용할 수 있습니다.

public function actionSendToReview($id) 
{ 
    if (Yii::$app->user->can('Salesperson')) 
    { 
     $model = $this->findModel($id); 
     if ($model->orden_stage_id == 1 && $model->sales_person_id == Yii::$app->user->identity->id) 
     { 
      $model->orden_stage_id = 2; 
      $model->date_modified = date('Y-m-d h:m:s'); 
      $model->modified_by = Yii::$app->user->identity->username; 



      //TODO: Validation logic if is not valid show validation errors 
      //for example "For sending to review this values are required: 
      //list of attributes in bullets" 
      //A preferred way would be to auto redirect to update action but 
      //showing the validation error and setting scenario to    
      //"send_to_review". 

      //optional 
      $model->scenario=//put here the scenario for validation; 

      //if everything is validated as per scenario 
      if($model ->validate()) 
      {     
       $model->save(); 
       $this::insertStageHistory($model->order_id, 2); 
       return $this->redirect(['index']); 
      } 
      else 
      { 
       return $this->render('update', [ 
       'model' => $model, 
       ]); 
      } 


     } 
     else 
     { 
      throw new ForbiddenHttpException(); 
     } 
    } 
    else 
    { 
     throw new ForbiddenHttpException(); 
    } 
} 

당신이 어떤 필드의 유효성을 검사하지 않는 actionCreate() .Create 시나리오에서 검증을 필요가 적용되지 않는 경우.

+0

잘 작동하지만 이제 유효성 검사 오류가 두 번 표시됩니다. 기묘한 –