简体   繁体   English

yii2如何将发布数据从一个视图转移到两个视图?

[英]yii2 How to transfer post data from one view to two?

I am trying to create make a two-step form in yii2. 我正在尝试在yii2中创建一个两步式表单。

This is my SiteController.php 这是我的SiteController.php

public function actionCreateCharacter()
{
    $model = new Character();
    var_dump(Yii::$app->request->post('Character'));
    if ($model->load(Yii::$app->request->post())) {
        $attributes=['imie','nazwisko','plec','wyznanie_id'];
        if ($step1 = $model->validate($attributes)) {
            //var_dump($step1);
            // form inputs are valid, do something here
            //var_dump(Yii::$app->request->post('Character');

            return $this->render('createCharacterStep2', [
                'model' => $model,
            ]);;
        }
        else {
            // validation failed: $errors is an array containing error messages
            $errors = $model->errors;
        }
    }

    return $this->render('createCharacter', [
        'model' => $model,
    ]);
}

public function actionCreateCharacterStep2()
{
    $model2 = new Character();
    var_dump($model);
    if ($model2->load(Yii::$app->request->post())) {
        var_dump(Yii::$app->request->post('Character'));
        if ($model2->validate()) {
            // form inputs are valid, do something here
            return;
        }
    }

    /*return $this->render('createCharacter2', [
        'model' => $model,
    ]);*/
}

... and this is my Character.php (model + attributeLabels and tableName) ...这是我的Character.php(模型+ attributeLabels和tableName)

public function rules()
{
    return [
        [['user_id', 'imie', 'nazwisko', 'plec', 'wyznanie_id', 'avatar_src', 'avatar_svg'], 'required'],
        [['user_id', 'wyznanie_id'], 'integer'],
        [['avatar_svg'], 'string'],
        [['imie'], 'string', 'max' => 15],
        [['nazwisko'], 'string', 'max' => 20],
        [['plec'], 'string', 'max' => 1],
        [['avatar_src'], 'string', 'max' => 30]
    ];
}

I have access to $_POST by Yii::$app->request->post() in createCharacter - I get imie , nazwisko , plec and wyznanie_id . 我可以通过createCharacterYii::$app->request->post()访问$_POST imie我得到了imienazwiskoplecwyznanie_id

But when I send the form in step 2 I have only post data from step 2. 但是,当我在第2步中发送表单时,我只有第2步中的过帐数据。

How can I set the post data from step1+step2? 如何设置步骤1和步骤2中的发布数据?

Sorry for my english and thanks in advance. 对不起,我的英语,谢谢。

There is another way , if you have to table for step 1 and step 2 . 还有另一种方法,如果您必须为step 1step 2表格。 then save the data of step 1 first then step2 data. 然后先保存步骤1的数据,然后再保存步骤2的数据。
if you are not using two tables then you can create two form each form for each step and also create scenarios for each step according to the fields. 如果不使用两个表,则可以为每个步骤创建两个表单,也可以根据字段为每个步骤创建scenarios
I think this may help . 我认为这可能会有所帮助。 You can use session also as per discussion in comments or you can use the extension array wizard
but array wizard extension is not well documented , so i suggest you try my way i will help you. 但是数组向导扩展没有很好的文档说明,所以我建议您尝试用我的方式为您提供帮助。

While rendering step2 from step1 action, you can always pass additional data to controller's action. 从step1操作渲染step2时,您始终可以将其他数据传递给控制器​​的操作。 So I added "STEPONEPOSTS" post variable which contains all posts of step 1. Check below. 因此,我添加了“ STEPONEPOSTS”帖子变量,其中包含步骤1的所有帖子。请检查以下内容。

public function actionCreateCharacter()
{
    $model = new Character();
    var_dump(Yii::$app->request->post('Character'));
    if ($model->load(Yii::$app->request->post())) {
        $attributes=['imie','nazwisko','plec','wyznanie_id'];
        if ($step1 = $model->validate($attributes)) {
            //var_dump($step1);
            // form inputs are valid, do something here
            //var_dump(Yii::$app->request->post('Character');

            return $this->render('createCharacterStep2', [
                'model' => $model,
                'STEPONEPOSTS' => Yii::$app->request->post(),
            ]);;
        }
        else {
            // validation failed: $errors is an array containing error messages
            $errors = $model->errors;
        }
    }

    return $this->render('createCharacter', [
        'model' => $model,
    ]);
}

And now in step 2 view, you can get step 1 posts variable as 现在在第2步视图中,您可以将第1步的发布变量设为

$STEPONEPOSTS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM