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

How to pass model to Listview in yii2
Is there better code generation for Yii2 other than Gii
Yii2: Registering javascript when loading (nested) Gridview through renderAjax
Yii2 set active user in API
Yii2 full calendar AjaxEvents not working properly
white color Issue after create thumbnail image in yii2
Creating a one-to-many relationship in Yii2
Handle Image update in yii2
Header and Footer missing in modal popup
Select2 Krajee widget - using modal and getting the id of the value
sjaakp/yii2-illustrated-behavior Yii2 extension for image not working
change the navigation bar url from yii2 without redirecting
Yii 2 - Conditional Assets?
How to get value of dropdown instead of id yii2
How can I access private value from object
How to add valueless attribute to <form> tag using yii2 ActiveForm

Categories

HOME
maven
arduino-uno
image
deployment
iot
spagobi
android-4.4-kitkat
routes
amortized-analysis
infragistics
disassembler
retrofit
ezpublish
session-variables
gorm
tomcat6
fancybox-3
wheelnav.js
correlation
resize
usergrid
static-libraries
dynamics-crm-online
worldwind
invantive-sql
reactcsstransitiongroup
physics-engine
functional-testing
arabic
pass-by-reference
autosys
django-storage
uninstall
filezilla
subdomains
gsmcomm
google-cloud-endpoints-v2
stacked
qwerty
hot-module-replacement
quadratic-programming
scrollable
zip4j
paxos
jlink
rdfs
typed-lambda-calculus
or-tools
pintos
messenger
gridpane
login-control
nsfilemanager
livequery
intellij-14
jmeter-maven-plugin
javax.mail
jscript.net
javafx-webengine
xcode-6.2
onactivityresult
mesa
typeof
dml
fluentautomation
jubula
mysql-error-1062
geos
jplaton
eclipse-memory-analyzer
kyotocabinet
datawindow
cascalog
jquery-ui-layout
doh
infobox
recent-documents
pyinotify
jmock
site.master
sortable-tables
ncqrs
thunderbird-lightning
data-driven
text-coloring
scripting-languages
grid-system
multiple-languages

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