2015-07-08 20 views
9

Tôi đang thêm trường biểu mẫu động trênChange của menu thả xuống. Cả hai loại trường đều đến từ các mô hình khác nhau và đi đến cơ sở dữ liệu trong các bảng khác nhau. Tôi đã xác định các quy tắc xác nhận trong các mô hình.YII2: Thêm trường biểu mẫu động và xác thực của chúng

Nhưng xác thực không hoạt động đúng cách. Mã của tôi là như sau:

mẫu:

<?php 

namespace common\models; 

use Yii; 

/** 
* This is the model class for table "{{%membership_features}}". 
* 
* @property integer $id 
* @property string $title 
* @property string $type 
* @property integer $is_new 
* @property integer $status 
* @property integer $is_deleted 
* @property string $created_date 
* @property string $modified_date 
* 
* @property MembershipFeaturesValue[] $membershipFeaturesValues 
*/ 
class MembershipFeatures extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 

    public $value=[]; 
    public static function tableName() 
    { 
     return '{{%membership_features}}'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['title', 'type', 'value','is_new', 'status'], 'required'], 
      ['value', 'each', 'rule' => ['integer']], 
      ['value', 'each', 'rule' => ['required']], 
      [['is_new', 'status', 'value','is_deleted'], 'integer'], 
      [['created_date', 'modified_date'], 'safe'], 
      [['title', 'type'], 'string', 'max' => 255] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => Yii::t('app', 'ID'), 
      'title' => Yii::t('app', 'Title'), 
      'type' => Yii::t('app', 'is boolean or value'), 
      'is_new' => Yii::t('app', 'Is New'), 
      'status' => Yii::t('app', 'Status'), 
      'is_deleted' => Yii::t('app', 'Is Deleted'), 
      'created_date' => Yii::t('app', 'Created Date'), 
      'modified_date' => Yii::t('app', 'Modified Date'), 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getMembershipFeaturesValues() 
    { 
     return $this->hasMany(MembershipFeaturesValue::className(), ['feature_id' => 'id']); 
    } 
} 

Bộ điều khiển:

<?php 

namespace backend\controllers; 

use Yii; 
use common\models\MembershipFeatures; 
use backend\models\MembershipFeaturesSearch; 
use yii\web\Controller; 
use yii\web\NotFoundHttpException; 
use yii\filters\VerbFilter; 
use yii\web\Response; 
use common\models\MembershipFeaturesValue; 
use common\components\Helper; 
/** 
* MembershipFeaturesController implements the CRUD actions for MembershipFeatures model. 
*/ 
class MembershipFeaturesController extends Controller 
{ 
    public function behaviors() 
    { 
     return [ 
      'verbs' => [ 
       'class' => VerbFilter::className(), 
       'actions' => [ 
        'delete' => ['post'], 
       ], 
      ], 
     ]; 
    } 

    /** 
    * Lists all MembershipFeatures models. 
    * @return mixed 
    */ 
    public function actionIndex() 
    { 
     $searchModel = new MembershipFeaturesSearch(); 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

     return $this->render('index', [ 
      'searchModel' => $searchModel, 
      'dataProvider' => $dataProvider, 
     ]); 
    } 

    /** 
    * Displays a single MembershipFeatures model. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionView($id) 
    { 
     return $this->render('view', [ 
      'model' => $this->findModel($id), 
     ]); 
    } 

    /** 
    * Creates a new MembershipFeatures model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
    * @return mixed 
    */ 
    public function actionCreate() 
    { 
     $model = new MembershipFeatures(); 
     $membershipPlan = \common\models\MembershipPlan::allPlans(); 

     if(isset($_GET['type'])){ 
      $model->type =$_GET['type']; 
     } 
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { 

      Yii::$app->response->format = Response::FORMAT_JSON; 
      return \yii\widgets\ActiveForm::validate($model); 
     } 

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

      if($model->save()){ 
       foreach ($membershipPlan as $key=>$value) { 
        $feature = new MembershipFeaturesValue(); 
        $feature->feature_id = $model->id; 
        $feature->plan_id = $key; 
        $feature->value =$model->value[$key]; 
        $feature->save(); 
       } 
      } 



      return $this->redirect(['index']); 
     } 
      return $this->render('create', [ 
       'model' => $model, 
       'membershipPlan'=>$membershipPlan, 

      ]); 

    } 

    /** 
    * Updates an existing MembershipFeatures model. 
    * If update is successful, the browser will be redirected to the 'view' page. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionUpdate($id) 
    {  
     $membershipPlan = \common\models\MembershipPlan::allPlans();  
     $model = $this->findModel($id); 

     $selected = MembershipFeaturesValue::find()->where(['feature_id'=>$model->id])->all(); 
     foreach ($selected as $key => $value) { 
       $model->value[$value->plan_id]=$value->value; 
     }  

     if(isset($_GET['type'])){ 
      $model->type =$_GET['type']; 
     } 
     if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { 

      Yii::$app->response->format = Response::FORMAT_JSON; 
      return \yii\widgets\ActiveForm::validate($model); 
     } 

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

      if($model->save()){ 

       foreach ($membershipPlan as $key=>$value) { 
        $feature = MembershipFeaturesValue::find()->where(['feature_id'=>$model->id,'plan_id'=>$key])->one(); 
        $feature->value =$model->value[$key]; 
        $feature->save(); 
       } 
      } 



      return $this->redirect(['index']); 
     } 

     return $this->render('update', [ 
       'model' => $model, 
       'membershipPlan'=>$membershipPlan, 

      ]); 

    } 

    /** 
    * Deletes an existing MembershipFeatures model. 
    * If deletion is successful, the browser will be redirected to the 'index' page. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionDelete($id) 
    { 
     Helper::partialDelete('MembershipFeatures',$id); 

     return $this->redirect(['index']); 
    } 

    /** 
    * Finds the MembershipFeatures model based on its primary key value. 
    * If the model is not found, a 404 HTTP exception will be thrown. 
    * @param integer $id 
    * @return MembershipFeatures the loaded model 
    * @throws NotFoundHttpException if the model cannot be found 
    */ 
    protected function findModel($id) 
    { 
     if (($model = MembershipFeatures::findOne($id)) !== null) { 
      return $model; 
     } else { 
      throw new NotFoundHttpException('The requested page does not exist.'); 
     } 
    } 
} 

Mẫu:

<?php 

use yii\helpers\Html; 
use yii\widgets\ActiveForm; 
use yii\widgets\Pjax; 
use yii\helpers\Url; 
/* @var $this yii\web\View */ 
/* @var $model common\models\MembershipFeatures */ 
/* @var $form yii\widgets\ActiveForm */ 
?> 

<div class="membership-features-form"> 

    <?php $form = ActiveForm::begin([ 
    'enableAjaxValidation' => true, 
    'enableClientValidation'=>true, 
    'validateOnSubmit'=>true, 
    'options' => ['data-pjax'=>true]]); ?> 
    <?= $form->errorSummary($model); ?> 
    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?> 

    <?= $form->field($model, 'type')->dropDownList(['boolean'=>'Boolean','value'=>'Value'], 
     [ 
     'onchange'=>' 
      $.pjax.reload({ 
      url: "'.Url::to(['create']).'?type="+$(this).val(), 
      container: "#pjax-memfeature-form", 
      timeout: 1000, 
      }); 
     ', 

     'class'=>'form-control', 
     'prompt' => 'Select Type Of Value' 
     ]) ?> 

     <?php Pjax::begin(['id'=>'pjax-memfeature-form','enablePushState'=>false]);  ?> 
     <?php 
      if($model->type==='boolean'){ 
       foreach ($membershipPlan as $key => $value) { 
        echo $form->field($model, "value[$key]")->checkbox(array(
           'label'=>"$value", 
           'labelOptions'=>array('style'=>'padding:5px;'), 

           )); 
       }  
      } 
      if($model->type==='value'){ 
       foreach ($membershipPlan as $key => $value) { 
        echo $form->field($model, "value[$key]")->textInput()->label("$value"); 
       } 
      } 
     ?> 

     <?php Pjax::end(); ?>   
    <?= $form->field($model, 'is_new')->dropDownList(['0'=>'No','1'=>'Yes']) ?> 

    <?= $form->field($model, 'status')->dropDownList(['1'=>'Active','0'=>'Inactive']) ?> 

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
     <?= Html::a(Yii::t('app', 'Cancel'), ['/membership-features/'], ['class' => 'btn btn-danger']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

</div> 

Tôi muốn xác nhận giá trị lĩnh vực của tôi mà là tự động thêm vào khi tôi thay đổi kiểu thả xuống bằng cách sử dụng Pjax. Vui lòng hướng dẫn tôi một phương pháp chính xác để xác thực các trường biểu mẫu được thêm động.

+0

thấy điều này, nó có thể giúp bạn: www.yiiframework.com/doc-2.0/yii-base-dynamicmodel.html –

+0

@AwesomeAP: cảm ơn cho y trả lời nhanh chóng của chúng tôi ... tôi đã đọc điều này, nhưng mô hình của tôi không phải là năng động ..thực sự các lĩnh vực của tôi về mô hình thành viênPlan được thêm động vào biểu mẫu trên. Vì vậy, hãy xây dựng nếu có thể thông qua lớp mô hình động. – WpTricks24

+0

Bạn đã giải quyết được vấn đề chưa? –

Trả lời

7

của nó được một tháng để đoán này đã được giải quyết nhưng đối với tài liệu tham khảo cho những người khác như tôi tìm kiếm giống nhau và để tiết kiệm phải bước qua khuôn khổ để tìm câu trả lời có lẽ thử một cái gì đó như:

... 
use yii\helpers\Json; 
... 

<?php foreach ($form->attributes as $attribute) { 
    $attribute = Json::htmlEncode($attribute); 
    $this->registerJs("jQuery('form').yiiActiveForm('add', $attribute);"); 
} ?> 

<?php Pjax::end(); ?> 
... 

Thử nghiệm liên quan đến clientValidation và không phải với câu hỏi trên vì vậy đã hacked giải pháp của tôi để hy vọng trả lời câu hỏi trên.

+0

Tìm thấy toàn bộ bài đăng này rất hữu ích cảm ơn bạn! Khi yiiActiveForm của tôi ('add' JS đang chạy, tôi đã nhận được $ form.data là lỗi không xác định - bọc yiiActiveForm ('add' trong sự kiện sẵn sàng của jQuery và bạn đã sẵn sàng! –

1

Điều đó không hoàn toàn phù hợp với tôi, tôi phải thêm các chức năng bổ sung và phân tách này cho js của mình. Tôi ajax tắt để có được một hàng mới mỗi lần sau đó tiêm nó vào DOM sau hàng cuối cùng.

// Thêm xác thực cho các phần tử đã nói (không có giá trị val.validate nhưng tôi đã có xác thực không phải là lỗi biểu thức) là đối tượng json của mô hình db đến từ hành động điều khiển ajax của tôi.

$.each(data.attributes, function(key, val) {     
val = $.parseJSON(val);   
// The validate method is kept in expression so turn it back into a closure. 
val.validate = eval("var f = function(){ return "+val.validate.expression+";}; f() ;") ; 
$('#dal-add-form').yiiActiveForm('add', val); 
}); 

Sau đó, để loại bỏ các xác nhận:

$.each($(this).parents('.dal-row').find('input, textarea, select'), function() {    
$('#form').yiiActiveForm('remove', $(this).attr('id')); 
});  
0

Simple

Bạn nên cố gắng này

<?php 
    $this->registerJs(' 



      jQuery("#w0").yiiActiveForm("add",{ 
       "id": "customer-name", 
       "name": "name", 
       "container": ".field-customer-name", 
       "input": "#customer-name", 
       "error": ".help-block.help-block-error", 
       "validate": function(attribute, value, messages, deferred, $form) { 

        yii.validation.required(value, messages, { 
         "message": "Name be blank bug." 
        }); 

        yii.validation.string(value, messages, { 
         "message": "Name must be a string.", 
         "max": 255, 
         "tooLong": "Name should contain at most 255 characters.", 
         "skipOnEmpty": 1 
        }); 
       } 
     }); 


    '); 
?> 

Changes

  • W0 vào ID mẫu của bạn

  • "id": "khách hàng-name" vào ID lĩnh vực đầu vào của bạn

  • "container": ".field-khách hàng-name" vào trường nhập div container lớp

Các vấn đề liên quan