4

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.

JoenMarz
  • 143
  • 3
  • 13

1 Answers1

2

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. :)

MH2K9
  • 11,951
  • 7
  • 32
  • 49
  • Almost work, now came the similar message; Undefined variable: model where/how do i define the $model? – JoenMarz Jul 15 '14 at 02:28
  • Please update answer, I haven't found the way to define the $model variable. – JoenMarz Jul 15 '14 at 12:49
  • @JoenMarz I updated my answer. just carefully follow the example part. You need to create the model object and assign it to a viriable ($form). eg,.$model=new ModelClassName; & then pass the variable to view file during render. eg, $this->render('viewFileName',array('model'=>$model)); – MH2K9 Jul 15 '14 at 13:58
  • If you don't understand I hope you need to start from http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices . That will be best way for you. :) – MH2K9 Jul 15 '14 at 14:09
  • I've updated my work, please check. Inside the controller(/protected/controllers/LayoutsController.php) already have the $model=new Item; and passed the variable to my view file during render: $this->render('main',array('model'=>$model)); but then the error message is still undefined variable: model – JoenMarz Jul 16 '14 at 21:18
  • 1
    Did you use render partial? if do that then need to pass the model object with this. Or you can simply create the object of model in view page. In your view file add this line $model=new Item; if your model class name is Item – MH2K9 Jul 17 '14 at 13:16
  • Ah that's it!Thanks! It works now. Anyway, If I already defined the my $model inside controller, why should I redefine again at the view? I'll be glad to have another brief explanation :) Thanks again. – JoenMarz Jul 18 '14 at 10:28