Checkbox in _form view file [merged duplicate post]

i need to put a checkbox in _form file of a model class.if there is a particular column in database table related to checkbox,it is appearing on the form.but i dont want to put any column for this checkbox in my database table.Also i need to put an onClick event for that checkbox.

i am using this code…

<div id="row">

<?php $form->labelEx($model,‘test’);

<?php $form->checkBox($model,‘test’);

<?php $form->error($model,‘test’);

</div>

this code gives an error "test" is not defined.if there is no column in database table for this checkbox

Plz help

Just alter your table like this:

ALTER table Foo add test tinyint default 0;

This will work like test is boolean value.

Then add test in your rules array in Model.

array(‘test’, ‘boolean’)

Then all should work.

@ [color="#284b72"]jeewendra_kumar[/color]

Note that you posted on wrong sub-forum "Yii-powered applications"

Moved to "General Discussion for Yii 1.1.x"

Modify your model class:




class MyModel extends CActiveRecord

{

    public $test;


    // ...

}



You can also add validation rules for this new field.

As you say, you dont need a database column, so you can use CHtml::[font=monospace]checkBox there you can put name which you wannt it’s not debend on your model becaus it is not asociated with model, if you chose CHtml::activeC[/font][font=monospace]heckBox then you must use your model and with simple [/font][font=monospace]checkBox it dont need to be used.[/font]

[font=monospace]

[/font]

[font=monospace]You can read more about this function http://www.yiiframework.com/doc/api/1.1/CHtml#checkBox ;)[/font]