Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Multiple Nested Forms with Yii and Bootstrap Extension
<?php
// Controller
//
// Company fields => address_id, idenity_id
//
public function actionCreate()
{
$model = new Company;
$model_address = new Address;
$model_identity = new Identity;
if(isset($_POST['Address']) and isset($_POST['Identity']))
{
$model_identity->attributes = $_POST['Identity'];
$model_address->attributes = $_POST['Address'];
if($model_address->save() and $model_identity->save())
{
$model->address_id = $model_address->id;
$model->identity_id = $model_identity->id;
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create', array('model' => $model, 'model_address' => $model_address, 'model_identity' => $model_identity));
}
public function actionUpdate($id)
{
$model = $this->loadModel($id);
$model_address = Address::model()->findByPk($model->address_id);
$model_identity = Identity::model()->findByPk($model->identity_id);
if(isset($_POST['Address']) and isset($_POST['Identity']))
{
$model_identity->attributes = $_POST['Identity'];
$model_address->attributes = $_POST['Address'];
if($model_address->save() and $model_identity->save())
{
$model->address_id = $model_address->id;
$model->identity_id = $model_identity->id;
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update', array('model' => $model, 'model_address' => $model_address, 'model_identity' => $model_identity));
}
// Company _form view
<p class="well"><?= Yii::t('app', 'required_field') ?></p>
<?php
$form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
'htmlOptions' => array('class'=>'well'),
'id' => 'user-form',
'enableClientValidation'=>true,
)); ?>
<fieldset>
<legend>Adresse</legend>
<?php $this->renderPartial('//addresses/_field_form', array('model' => $model_address, 'form' => $form));?>
<fieldset>
<fieldset>
<legend>Identité</legend>
<?php $this->renderPartial('//identities/_field_form', array('model' => $model_identity, 'form' => $form));?>
</fieldset>
<?php $this->renderPartial('//shared/_form_button', array('label' => $model->isNewRecord ? 'Création' : 'Sauvegarde'));?>
<?php $this->endWidget(); ?>
// View //addresses/_field_form'
<?php if(Yii::app()->request->isPostRequest) $model->validate(); // Je revalide pour recharger les messages d'erreurs ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model, 'address'); ?>
<?php echo $form->textFieldRow($model, 'address_comp'); ?>
<?php echo $form->textFieldRow($model, 'cp'); ?>
<?php echo $form->textFieldRow($model, 'city'); ?>
<?php echo $form->textAreaRow($model, 'comment'); ?>
// View //identities/_field_form'
<?php if(Yii::app()->request->isPostRequest) $model->validate(); // Je revalide pour recharger les messages d'erreurs ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model, 'firstname'); ?>
<?php echo $form->textFieldRow($model, 'lastname'); ?>
<?php echo $form->textFieldRow($model, 'company'); ?>
<?php echo $form->textFieldRow($model, 'phone'); ?>
<?php echo $form->textFieldRow($model, 'phone1'); ?>
<?php echo $form->textFieldRow($model, 'phone2'); ?>
<?php echo $form->textFieldRow($model, 'fax'); ?>
<?php echo $form->textFieldRow($model, 'email1'); ?>
<?php echo $form->textFieldRow($model, 'email2'); ?>
<?php echo $form->textFieldRow($model, 'civilite_id'); ?>
// View //shared/_form_button'
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'submit', 'type'=>'primary', 'icon'=>'ok white', 'label'=> $label )); ?>
<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'reset', 'icon'=>'remove', 'label'=>'Annuler')); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment