yii2 formwizard选择2后更改模型

yii2 formwizard选择2后更改模型,yii2,yii2-formwizard,Yii2,Yii2 Formwizard,我正试图使用这个小部件实现一个多模型表单,其中Profile作为主模型,其他几个作为链接模型。当我为主模型选择“图元类型”字段时,我希望根据“图元类型”字段的值为下一步更改链接模型。 我已尝试使用此代码: Create Form Code $modelUrlReletedModelsCreate = Profile::urlRelatedModelCreate(); $urlLinkedProfile = Url::to(['create']); echo FormWizard::widg

我正试图使用这个小部件实现一个多模型表单,其中Profile作为主模型,其他几个作为链接模型。当我为主模型选择“图元类型”字段时,我希望根据“图元类型”字段的值为下一步更改链接模型。 我已尝试使用此代码:

Create Form Code


$modelUrlReletedModelsCreate = Profile::urlRelatedModelCreate();
$urlLinkedProfile = Url::to(['create']);

echo FormWizard::widget([
'formOptions'=>[
    'id'=>'profile_form',
    'enableClientValidation'=>true,
    'enableAjaxValidation'=>true,
    'validationUrl' => Url::to(['profile-models-validation'])

],
'theme' => FormWizard::THEME_MATERIAL_V,
'steps' => [
    //step 1 
    [
        'model' => $model,
        'title' => \Yii::t('app', 'Profile'),
        'fieldConfig' => [
            'only' => ['entity_type_id', 'profile_type_id'], 
            'entity_type_id' => [
                'widget' => Select2::class,
                'options' => [
                    'data' => EntityType::arrayNamesList(),
                    'options' => ['placeholder' => \Yii::t('app','Select an element')],
                    'pluginEvents' => ['select2:select'=>'function(e){;'
                                            . 'var type = $("#pr-ty-sel").val();'
                                            . 'var profile= "'.($model->profile_id ? : "no").'";'
                                            . '$.ajax({ method: "GET",'
                                                    . 'url:"'.$urlLinkedProfile.'",'
                                                    . 'data:{ entity_text : e.params.data.text,'
                                                            . 'entity_id : e.params.data.id,'
                                                            . 'profile_type_id : type,'
                                                            . 'profile_id : profile},'
                                                    . 'success: function(data){'
                                                        . '$("#profile_form").html(data);'
                                                    . '}'
                                                  . '});}'],
                ],

            ],
            'profile_type_id' =>[
                'widget' => Select2::class,
                'options' => [
                    'data' => \app\models\ProfileType::arrayNamesList(),
                    'options' => ['placeholder' => \Yii::t('app','Select an element'),'id'=>'pr-ty-sel'],
                ],

            ]
        ],
        'description' => \Yii::t('app', 'Add Profile Type Data'),
        'formInfoText' => \Yii::t('app', 'Fill all fields'),
    ],

    //step 2 I want ot change here $linkedModel 
    [
        'model' => [$model,$linkedModel],
        'title' => \Yii::t('app', 'Personal Data'),
        'description' => \Yii::t('app', 'Insert Personal Data'),
    ],
]
]);

Controller Action Create Code

public function actionCreate($profileId = NULL)
{

    if($profileId AND $profileId !== 'no'){
        $model = $this->findModel($profileId);
    }else{
        $model = new Profile();
    }

    $profileLinkedModel = new ProfilePrivate();
    $renderMethod = 'render';
    if (Yii::$app->request->isAjax) {
        $entityText = \Yii::$app->request->get('entity_text');
        $entity_id = \Yii::$app->request->get('entity_id');
        $profileTypeId = \Yii::$app->request->get('profile_type_id');
        $profileId = \Yii::$app->request->get('profile_id');

        //Utility function to clean the entity text (remove number and special characters)
        $entityTextCleaned = \app\components\Utility::cleanString($entityText);

        if ($entityTextCleaned == 'private') {
            $profileLinkedModel = new ProfilePrivate();
        } elseif ($entityText == 'company') {
            $profileLinkedModel = new ProfileCompany();
        } else {
            //@TODO 
            return FALSE;
        }

        $model->entity_type_id = $entity_id;
        $model->profile_type_id = $profileTypeId;


        $profileLinkedModel->profile_id = $model->profile_id;
        Yii::$app->response->format = Response::FORMAT_JSON;

        $renderMethod = 'renderAjax';
    }

    //extra table field used to enable custom rule in model
    $model->createFullProfile = TRUE;

    if ($model->load(Yii::$app->request->post())) {
        return $this->redirect(['view', 'id' => $model->profile_id]);
    }

    return $this->$renderMethod('create', [
        'model' => $model,
        'profileLinkedModel' => $profileLinkedModel,
    ]);
}

当我为EntityTypeField选择一个字段时,服务器在select事件上运行ajax请求,但当它结束时,另一个表单字段就不再是可选的了。因此,换句话说,在ajax请求之后,我无法选择profile type字段。如果我尝试在实体类型字段之前选择概要文件类型字段,我可以进入下一步,但始终加载默认模型

不清楚您在问什么,您已经为实体类型定义了pluginEvent,并且在第一行“select2:select'=>”函数{;'上有一个输入错误,后面不应该有一个{然后在success method中有一行“$profile_form.htmldata;”,这是表单的id,意味着您正在覆盖完整表单的html,这不是一件明智的事情,您的要求是什么,您试图做什么。关于选择select2中的任何选项,您试图实现什么?很抱歉,不清楚r、 我的目的是根据实体类型字段的选定值更改链接模型。因此,例如,如果我选择“Private”作为实体类型,我将加载ProfilePrivate模型作为链接模型,如果我选择“Company”,则将加载ProfileCompany。我尝试在第二步中根据en重新加载带有新链接模型的表单tity type选择了值,但失败。但是;是已删除行console.loge的一部分。我已在id为向导的div中添加了formwziard内部表单注册表。成功回调函数现在推送ajax请求$wizard-internal-form-registry.htmldata;返回的数据。现在表单似乎正常工作。但是,当我到达最后一页时我点击“Finish”按钮,发现以下javascript错误:TypeError:$.formwizard.fields[a][o]未定义。我不知道此错误来自何处。我想说的是,您不能通过javascript更改模式,或者您的意思是要覆盖与该模型相关的表单字段?理想情况下,作为小部件的所有者/开发人员,我不建议您这样做,因为forwizard.js在jso中收集字段IDn数组,需要更新。您在上一条消息中提到的错误与我所说的有关,您可能需要通过将javascript文件FormWizard.min.js的FormWizard资产类中的源文件更改为FormWizard.js来确认