yii2


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

Related Links

Why does CSRF get validated when executing console command in Yii2
Yii2: Add 'user_id' when create a post
output of active view depends on the type of user
How to set-up different URLs for yii frontend and backend
Ecommerce/Classified template with Yii2
Yii2 field accessed only via magic method
How to add a css class to <td> tags in a kv- GridView class in Yii2
Capcha always show incorrect
Integration Reportico in Yii2 Projects
Yii2 : Subdirectory in yii2 not working
Yii2 KCFinder with CKEditor
Yii2 - validation compare acting strange
Updating composer throws exception Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
Dynamic declaration of a model in Yii2
image are save in folder but not saved in database in yii2.0
how can i put sidebar menu in right side using yii2

Categories

HOME
blogger
gis
spring-jdbc
leon
cakephp-2.5
disassembler
cross-browser
uber-api
bs4
qore
maude-system
circular-dependency
usergrid
leiningen
hex-editors
zebra-printers
netflix
django-simple-history
graphlab
connection-string
lucene.net
grails3
one-hot-encoding
extjs5
format-specifiers
hammerspoon
su
react-css-modules
nhapi
gitignore
subdomains
bpel
skia
key-value-observing
android-nestedscrollview
ghost4j
y86
wpf-controls
bytecode-manipulation
appfabric
restlet
splice
firebase-admin
nand2tetris
nxlog
smartcontracts
azure-application-gateway
multipeer-connectivity
ionicons
mu
crypt
cudafy.net
imanage
infix-notation
adxstudio-portals
asp.net-4.5
mongocsharpdriver
fuzzy-search
gridview-sorting
websitepanel
lua-5.1
testng-dataprovider
pagerank
itextpdf
freelancer.com-api
uid
tcpserver
execute
wordml
fouc
content-length
dukescript
inmobi
umbraco6
facebook-graph-api-v2.4
asp.net-web-api-odata
php-5.4
terminfo
dml
concurrent-collections
stxxl
viadeo
html5-notifications
prettify
ms-project-server-2010
eclipse-memory-analyzer
django-nonrel
random-seed
gdata-api
multipage
dotnethighcharts
cufon
custom-backend
deobfuscation
hirefire
gdlib
fixed-width
mtj
zend-decorators
data-acquisition

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App