6

I'm creating a form view and I want to organize the form fields with tabs structure, using the official Tabs widget.

Is it possible init the Tabs widget with the id (or class) of the div elements that contains the active form fields?

userlond
  • 3,632
  • 2
  • 36
  • 53
AleCat83
  • 1,443
  • 3
  • 22
  • 41
  • your question is unclear and ambiguous, pleas explain in detail what exactly you want to do – deacs Feb 02 '15 at 13:34
  • Hi, I need to create a tabs widget but I need to init the content of each tabs with a div in the view. – AleCat83 Feb 02 '15 at 13:47
  • Why not have the form wrap around the tabs and you can just submit the entire thing. If you need validation for each tab then you can use scenarios to validate the same model on each post of the form. – Mihai P. Feb 02 '15 at 23:17
  • Hi, it seems ok, but how can I insert a TabsWidget wrapepd around the form? – AleCat83 Feb 04 '15 at 13:14
  • Have the same issue, does anybody know solution? – userlond Feb 23 '15 at 10:46

3 Answers3

6

One example of how you can manage it is doing like this:

  1. First, divide your contact-form into one view-file for each tab.
  2. Place the ActiveForm::begin() and ActiveForm::end() around the Tabs::widget()
  3. Render the contact-form pages into content, with parameters $model and $form

Example code:

views/site/contact.php

<?php

/* @var $this yii\web\View */
$this->title = 'Contact';

use yii\bootstrap\Tabs;
use yii\bootstrap\ActiveForm;
?>


<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= Tabs::widget([
        'items' => [
            [
                'label' => 'One',
                'content' => $this->render('contact_form1', ['model' => $model, 'form' => $form]),
                'active' => true
            ],
            [
                'label' => 'Two',
                'content' => $this->render('contact_form2', ['model' => $model, 'form' => $form]),
            ],
        ]]);
 ?>
    <?php ActiveForm::end(); ?>

views/site/contact_form1.php

<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>

views/site/contact_form2.php

<?php
use yii\helpers\Html;
use yii\captcha\Captcha;
?>

<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div    class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>

Hope this helps!

Jørgen
  • 3,467
  • 6
  • 33
  • 49
0

Just add at the top of your contact.php global $form; and all works fine.

0

I have another solution:

When we call $form->field($model, 'name')->textInput(), it will return the model of class yii\widgets\ActiveField, so just continue calling a method of this class as $form->field($model, 'name')->textInput()->render(). It will return a string then you can use it for the tab's content. I have an example code in my application for translating multi languages as the following code:

<?php
$items = [];
foreach ($translateModels as $translateModel) {
    $tabContent = $form->field($translateModel, "[{$translateModel->code}]name")->textInput()->render();
    $items[] = [
        'label' => $translateModel->language->name,
        'content' => $tabContent,
    ];
}
?>
<?= Tabs::widget([
    'options' => [
        'class' => 'nav-tabs',
        'style' => 'margin-bottom: 15px',
    ],
    'items' => $items,
]) ?>

Maybe it's help.

Tín Phạm
  • 642
  • 1
  • 6
  • 16