Implementando un formulario tipo Wizard con un formulario Tabular
Vamos a estar unificando varios formularios en uno, y además utilizando el formulario tabular de esta extensión:
1.- Instalar widget de formwizard, añadir al composer.json:
"buttflattery/yii2-formwizard":"@dev"
1.1.- Ejecutar en el cmd de la carpeta del proyecto:
composer update --prefer-dist
2.- Colocar en el form el use del widget:
use buttflattery\formwizard\FormWizard;
3.- Colocar el form:
<?= FormWizard::widget(
[
'formOptions' => [
'id' => 'my_form_tabular'
],
'theme' => FormWizard::THEME_ARROWS,
'labelNext' => Yii::t('app', 'Next'),
'labelPrev' => Yii::t('app', 'Previous'),
'labelFinish' => Yii::t('app', 'Finish'),
'steps' => [
[
'model' => $author,
'title' => Yii::t('app', 'Author'),
'description' => Yii::t('app', 'Add Authors'),
'formInfoText' => Yii::t('app', 'Add all fields')
],
[
'model' => $book,
'title' => Yii::t('app', 'Books'),
'description' => Yii::t('app', 'Add Books'),
'formInfoText' => Yii::t('app', 'Add all books'),
//set step type to tabular
'type' => FormWizard::STEP_TYPE_TABULAR,
'fieldConfig' => [
]
],
]
]
);
?>
4.- Ir al controlador y modificar el actionCreate:
public function actionCreate()
{
$model = new AuthorBook();
$author = new Author();
$book = [new Book()];
if ($author->load(Yii::$app->request->post()) && $author->save() ) {
$count = count(Yii::$app->request->post('Book',[]));
for ($i=1; $i < $count ; $i++) {
$book[]= new Book();
}
if (
Model::loadMultiple($book, Yii::$app->request->post())
&& Model::validateMultiple($book)
) {
foreach ($book as $value) {
$modelBook = new Book();
$modelBook->attributes = $value->attributes;
$modelBook->save();
$model = new AuthorBook();
$model->book_id = $modelBook->id;
$model->author_id = $author->id;
$model->save();
}
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'author' => $author,
'book' => $book,
]);
}
5.- Modificar el actionUpdate:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$author = $model->author;
$book = Book::find()
->innerJoin('author_book','author_book.book_id = book.id')
->where(['author_book.author_id'=>$model->author_id])->all();
$countBook = count($book);
if ($author->load(Yii::$app->request->post()) && $author->save()) {
$count = count(Yii::$app->request->post('Book',[])) - $countBook;
for ($i=0; $i < $count ; $i++) {
$book[]= new Book();
}
if (
Model::loadMultiple($book, Yii::$app->request->post())
&& Model::validateMultiple($book)
) {
AuthorBook::deleteAll(['author_id'=>$author->id]);
foreach ($book as $value) {
if($value->id != null){
$modelBook = $value;
}else{
$modelBook = new Book();
}
$modelBook->attributes = $value->attributes;
$modelBook->save();
$model = new AuthorBook();
$model->book_id = $modelBook->id;
$model->author_id = $author->id;
$model->save();
}
return $this->redirect(['index']);
}
}
return $this->render('update', [
'model' => $model,
'author' => $author,
'book' => $book,
]);
}
6.- Añadir el use en el controller:
use yii\base\Model;
use backend\modules\bookstore\models\Author\Author;
use backend\modules\bookstore\models\Book\Book;
7.- Modificar el archivo views/author-book/create.php :
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\bookstore\models\AuthorBook\AuthorBook */
$this->title = Yii::t('app', 'Create Author Book');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Author Books'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="author-book-create">
<?= $this->render('_form', [
'model' => $model,
'author' => $author,
'book' => $book,
]) ?>
</div>
8.- Modificar el archivo views/author-book/update.php :
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\bookstore\models\AuthorBook\AuthorBook */
$this->title = Yii::t('app', 'Update Author Book: {name}', [
'name' => $model->id,
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Author Books'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('app', 'Update');
?>
<div class="author-book-update">
<?= $this->render('_form', [
'model' => $model,
'author' => $author,
'book' => $book,
]) ?>
</div>
9.- Añadir los mensajes en common/messages/app:
'Author Books' => 'Autor Libros',
'Name' => 'Nombre',
'Add Authors' => 'Adicionar Autor',
'Add all fields' => 'Adicionar todos los campos',
'Next' => 'Próximo',
'Previous' => 'Anterior',
'Create Author Book' => 'Crear Autor Libro',
'Update Author Book' => 'Modificar Autor Libro',
10.- Revisar la documentación de la extensión:
https://yii2plugins.omaraslam.com/formwizard/docs
Tiene muchos ejemplos de como implementar y está muy bien explicada toda la documentación.
Puedes ver todo el desarrollo de la implementación en mi canal de YouTube: https://youtu.be/rtQlM1C00D4
Comentarios
Publicar un comentario