how to define CheckBoxList Rule

i use follow code define a checkbox list


echo CHtml::CheckBoxList('platform[]',null,array(0=>"1",1=>"2"));

and in the model,i wrote this


public function rules()

    {

        return array(

        array('platform','required')

        );

    }

but i doesn`t work,tell me "xxx.platform" not defined!

how should i set the require rule,help~

did you already add var and make it public in the model class?


public $platform;

ok.it has gone!

but after i submit, stile show the erro summary

platform can not be empty.I`ve check the item already,anyone know why?

you could use print_r() before save to see the data send via $_POST.

sory, i forget to ask you, did you defined ‘platform’ in database table as field?

if so, my previous post is not require.

no,not define in db.




platform can not be empty.I`ve check the item already,anyone know why? 



its because you define it in rules as required




public function rules()

    {

        return array(

        array('platform','required')

        );

    }



my suggest just print_r the $_POST data.


print_r($_POST['table_name'])

put this on actonCreate or actionUpdate in your controler.

if still not resolve, you could show your code.

:)

thks, i do it in this way ,made a hidden textfield name platform,and set the hidden textfield required,when check the checkbox,assign the value to the hidden textfield, but i think this is a stupid method,but anyway i can go on~ :lol:

If you set "required" for a checkbox, the user will be requred to check it.

Use:




public function rules()

    {

        return array(

        array('platform','safe')

        );

    }



‘safe’ will make the SetAttribute to collect the user input, but the textfield will not be requred.