Yii2 使用 yii2-formwizard 保存表格数据 [英] Yii2 Saving tabular data using yii2-formwizard

查看:23
本文介绍了Yii2 使用 yii2-formwizard 保存表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在集成 yii2-formwizard 以进行表格输入,但无法将数据提交给我的控制器动作并使用 Model::loadMultiple.

I am integrating yii2-formwizard for tabular input but unable to submit the data to my controller action and use Model::loadMultiple.

我必须将我的模型声明为数组,然后我需要在传递给视图之前初始化它,并且在 buttflattery/yii2-formwizard 前端我必须将我的模型指定为数组,但我可以不从我的控制器动态检索数据.

I have to declare my model as array and then I need to initialize it before passing to view and in buttflattery/yii2-formwizard front-end I must specify my model as array fine but I can not retrieve data from my controller dynamically.

我需要从前端动态创建实例并将它们保存在后端.我只能保存我从控制器初始化的实例.如果我不只初始化第一个保存的实例.而且当我使用 for 循环初始化多个实例时,前端会一次复制所有实例,这又是我不需要的.

I need to dynamically create instance from front-end and save them in back end. I can save only the instance I have initialize from my controller.if I do not initialize only first one instance saved.and also when I initialize multiple instance using for loop, front end replicate for all instance at once which again I do not need.

//controller
public function actionCreatemulti()
    {
        $this->layout='layout2';

        $education = [new Edusubject()];
            //## initialize array for 2 element (if I not initialize only one object pass or saved)
           for ($i=0; $i < 2 ; $i++) { 
             $education[]= new Edusubject();
        }

        //## Model::loadMultiple --> works only if $education is declared as array
        if (Model::loadMultiple($education, Yii::$app->request->post()) && Model::validateMultiple($education)) {
            foreach ($education as $edu) {
                $edu->save(false);
            }
            return $this->render('dummy');
        }

        return $this->render('createmulti', [
            'education' => $education,    
        ]);
}           

我的视图中的 FormWizard 代码

FormWizard code in my View

    <?php

    echo FormWizard::widget(
        [
            'formOptions' => [
                'id' => 'my_form_tabular'
            ],
            'steps' => [
                [
                //should be a single model or array of Activerecord model objects but for a single model only see wiki on github
                    'model' => $education, //## here I canot declared array again as I pass an array alredy from controller

                //set step type to tabular
                    'type' => FormWizard::STEP_TYPE_TABULAR,

当我在我的视图中将模型声明为数组时,我可以获得 wiki 但我无法保存这个数组,因为我无法实现 yii2 收集表格输入,另一方面,如果我将模型声明为数组并将其初始化并发送到前端那么形式不是动态的.它以表单形式显示所有实例,因此我不需要按我不需要的添加"按钮.

when i declare model as array in my view i can get dynamic form as described in wiki but I can not save this array since I can not implement yii2 Collecting tabular input as described, on the other hand if I declare model as array and initialize it and send to front end then form is not dynamic. it is shown all instance in form so I need not press "add" button, which I do not need.

推荐答案

我开发了这个小部件,但在我建议您之前,您应该阅读有关表格输入的基本实现的任何内容,尽管该指南并不完全有用,但有些部分是仍然在 TBD 下,并且尚未添加用于将表格数据插入/创建表格数据到表中的代码示例,但最好查看源方法,毕竟我们是工程师,我们应该能够理解框架核心的一部分或单独文件的任何功能的实现.

I developed this widget but before i suggest you anything you should read about the basic implementation of the tabular inputs although the guide is'nt completely helpful there are portions that are still under TBD and code sample for inserting/creating tabular data into the table isnt added yet with that much detail, but it is always better to look into the source method, after all we are Engineers and we should be able to understand the implementation of any function either a part of framework core or of a separate file.

现在关于这个问题,您没有理由使用您在操作之上添加的 for 循环

Now about the issue, you have no reason to use the for loop that you have added on top of your action

 for ($i=0; $i < 2 ; $i++) { 
     $education[]= new Edusubject();
 }

您从此处 并按原样复制粘贴到您的代码 ¯\_(ツ)_/¯ 中.

You took that part from the guide here and copy-pasted it as is in your code ¯\_(ツ)_/¯ .

指南中的这部分代码仅用于理解应如何填充表格模型数组,然后在创建新记录时提供用于加载和验证.

This part of code in the guide is for the understanding only how the tabular models array should be populated and then provided for loading and validating while creating new records.

我们需要通过

它们都将 $modelsarray 作为第一个参数,它应该保存要加载/验证的模型的实例.

Both of them take $modelsarray as 1st parameter which should hold the instances of the models to be loaded/validated.

对于 loadMultiple($models, $attributes) 记住,它会将 $attributes 数组中指定的所有属性加载到 $attributes 数组中指定的每个模型中>$models 数组,所有这些模型都需要属于同一类.$attributes 数组可以是 $_POST$_GET 或任何其他有效数组之一,有关详细信息,请参阅文档.

For loadMultiple($models, $attributes) remember that it will load all the attributes specified in the $attributes array into each of the models specified in the $models array, and all of those models need to be of the same class. the $attributes array can be one of $_POST or $_GET or any other valid array, see the docs for details.

对于validateMultiple($models),它可以是不同的模型,也可以是相同的,它会在$models中的每个模型上调用validate()> 数组.

For validateMultiple($models) it can be different models or same, it will call validate() on each model in the $models array.

所以你需要把它改成下面

So you need to change it to below

public function actionCreatemulti()
    {
        $this->layout='layout2';

        $education = [new Edusubject()];

        //cehck if post request
        if(Yii::$app->request->isPost){
            //get total models submitted
            $count = count(Yii::$app->request->post('Edusubject',[]));

            //start the loop from 1 rather than 0 and use the $count for limit
            for ($i=1; $i < $count ; $i++) { 
               $education[]= new Edusubject();
            }

            if (
                 Model::loadMultiple($education, Yii::$app->request->post()) 
                 && Model::validateMultiple($education)
            ) {
                foreach ($education as $edu) {
                    $edu->save(false);
                }
                return $this->render('dummy');
            }
        }

        return $this->render('createmulti', [
            'education' => $education,    
        ]);
}

在您的视图中,FormWizard 中的 model 属性将如下所示

and in your view the model property in the FormWizard will look like below

'model' => $education,

我刚刚在本地主机上对其进行了测试,它可以正确加载、验证和保存.

I just tested it on localhost and it loads, validates and saves correctly.

这篇关于Yii2 使用 yii2-formwizard 保存表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆