简体   繁体   中英

yii2 How to transfer post data from one view to two?

I am trying to create make a two-step form in yii2.

This is my 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)

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 .

But when I send the form in step 2 I have only post data from step 2.

How can I set the post data from step1+step2?

Sorry for my english and thanks in advance.

There is another way , if you have to table for step 1 and step 2 . then save the data of step 1 first then step2 data.
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.
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. So I added "STEPONEPOSTS" post variable which contains all posts of step 1. Check below.

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

$STEPONEPOSTS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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