-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling multiple models with a single form #3306
Comments
Please use our forum to ask questions. Thanks. |
This is not a question for the forum, there is a way to use loadMultiple to update, not to create more, should have documentation or create some method in model.php for multiple forms using loadMultiple. |
I see. I didn't understand your initial post. Re-opened for doc. |
My example for two methods public function actionCreate() {
$model = [];
$model[] = new Settings();
$postData = Yii::$app->request->post();
if ($postData) {
foreach ($postData as $i => $single) {
$model[$i] = new Settings();
}
}
if (Model::loadMultiple($model, Yii::$app->request->post())) {
$validate = false;
foreach ($model as $index => $setting) {
/*valitade first form*/
if($index==0){
if($setting->validate($setting->load(Yii::$app->request->post()))){
$validate = true;
}else{
/*return message validation here*/
}
}
if($validate==true){
if($setting->save(false)){
$save = true;
}
}
}
if($save==true)
{
/*return save message here*/
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionUpdate($id)
{
$model = Settings::find()->indexBy('id')->all();
if (Model::loadMultiple($model, Yii::$app->request->post())) {
$validate = false;
foreach ($model as $index => $setting) {
/*valitade first form*/
if($index==0)
{
if($setting->validate($setting->load(Yii::$app->request->post()))){
$validate = true;
}else{
/*return message validation here*/
}
}
if($validate==true){
if($setting->save(false)){
$save = true;
}
}
}
if($save==true)
{
/*return save message here*/
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
} |
http://www.yiiframework.com/forum/index.php/topic/53935-subforms/page__view__findpost__p__248184------------------ this example is best for docs |
This example is good for 2 or more models, great. |
Probably It can will be closed. https://github.com/yiisoft/yii2/blob/master/docs/guide/input-multiple-models.md |
https://github.com/yiisoft/yii2/blob/master/docs/guide/input-multiple-models.md This document don't have example with create new. |
What about dinamic count of each model? It is posible? |
@sieulog @StagnantIce please open new issues for this including more detailed description about your case. |
In controller:
In view:
$(function(){
}); Not user friendly. |
Possible use in actionCreate?
The text was updated successfully, but these errors were encountered: