phpyiiyii-extensionsyii-booster

How to put yiibooster's textField inside /view/layout/main.php in Yii


I'm using YiiBooster and trying to make a TextField within TbActiveForm inside the /views/layouts/main.php

For the controller I've added:

<?php
    class LayoutsController extends Controller
    {
        public function actionMain()
        {
                    $model=new Item;
                    $this->render('main',array('model'=>$model));
        }
    }
?>

And the views:

<?php 
 $this->beginWidget(
     'booster.widgets.TbActiveForm',
           array(
               'id' => 'inlineForm',
               'type' => 'inline',
                'htmlOptions' => array('class' => 'well'),
           )
      );
 echo $form->textFieldGroup($model, 'textField');
 $this->endWidget();
?>

But I have small problem, when tried to run it comes the error message:

PHP notice
Undefined variable: model

Can anybody help me to fix this? Thanks.


Solution

  • Point: 1

    If you only use $this->widget then the input elements of the form (eg., textFields, textAreas, dropdownlists, checkBoxes etc) will be placed out side the form. like

    <form method="post" action="#">
    
    </form>
    <!-- and then the input elements of form, like -->
    <input type="text" name="textField"> <!-- and so on.... -->
    

    Point: 2

    For including the elements inside the form you must start with

    $this->beginWidget
    // then the input elements , and finally
    $this->endWidget();
    

    So now the HTML look like

    <form method="post" action="#">
        <input type="text" name="textField"> <!-- and so on.... -->
    </form>
    

    Point: 3

    You must assign the beginWidget to a variable ($form) for including the inputs elements

    An Example below

    (i) In controller function

    public function actionFunctionName()
    {
         $model=new ModelClassName;
         $this->render('viewFileName',array('model'=>$model));
    }
    

    (ii) In view file

    <?php
    $form=$this->beginWidget(
        'booster.widgets.TbActiveForm',
        array(
            'id' => 'inlineForm',
            'type' => 'inline',
            'htmlOptions' => array('class' => 'well'),
        )
    );
    echo $form->textFieldGroup($model, 'textField');
    // before the close tag of php
    $this->endWidget();
    ?>
    

    It works fine.

    Point: 4

    If it doesn't work for you then check your YiiBooster configuration. I hope it will be helpful for you. :)