prawee / yii2-dependform-widgets
How to using relations model in single form
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-widget
Requires
- yiisoft/yii2: ~2.0.14
- yiisoft/yii2-bootstrap4: ~2.0.8
This package is auto-updated.
Last update: 2024-12-25 14:09:25 UTC
README
Example us case
Have 2 table need to save as the same time and depend on you pick amount of member of team
team - id - name - member person - id - team_id - name - position - experience
Usage
View
use prawee\widgets\DependWidget; ... $form = ActiveForm::begin(['id' => 'depend-form']); echo $form->field($team, 'name', ['options' => ['class' => 'col-md-8']])->textInput(['autofocus' => true]); echo $form->field($team, 'member', ['options' =>['class' => 'col-md-4']])->dropDownList([ 1 => 1, 2 => 2, 3 => 3, 5 => 5, 8 => 8, 10 => 10, ],['prompt' => 'select']); echo DependWidget::widget([ 'form' => $form, 'model' => $person, 'attributes' => [ 'name', 'position', 'experience' => [ 'type' => 'select', 'list' =>[1 => '1-2', 2 => '3-5', 3 => '5 >', 4 => '10 >'] ], ], 'attributeOptions' => ['class' => 'col-md-4'], 'depends' => [Html::getInputId($team, 'member')] ]); ?> <div class="form-group col-md-12"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?> </div> <?php ActiveForm::end(); ?>
Controller
... public function actionCreate() { $team = new Team(); $person = new Person(); $post = \Yii::$app->request->post(); if (Model::loadMultiple([$person], $post) && Model::validateMultiple([$person])) { $team->load($post['Team']); foreach($post['Person'] as $person): $person->load($person); $person->team_id = $team->id; $person->save(); endforeach; $team->save(); return $this->redirect(['index']); } return $this->render('create', [ 'team' => $team, 'person' => $person ]); } ...