yii,CactiveForm复选框列表

时间:2013-12-28 04:01:06

标签: php validation yii checkboxlist yii-cformmodel

我真的试图渲染一个checkboxlist,从来没有尝试过它,所以我遇到了问题,我试图渲染所有的复选框foreach列或属性,这是动作控制器:

public function actionCreate()
    {
        $model=new Parametro;

        $variable = file_get_contents('protected\column.txt');
        $vars = explode(' ', $variable);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Parametro']))
        {
            $model->attributes=$_POST['Parametro'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
            'variable'=>$vars,
        ));
    }

这是另一些方法的模型

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    $variable = file_get_contents('protected\column.txt');
    return array(
        array('parametro_id', 'required'),
        array('parametro_id', 'numerical', 'integerOnly'=>true),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, parametro_id', 'safe', 'on'=>'search'),
        array($variable, 'safe', 'on'=>'search'),   
    );
}
public function generateAttributeLabel($variable = null)
{
    if($variable)
    {
        $variable = file_get_contents('protected\column.txt');
    }
    return $variable;
}   

public function attributeLabels()
{
        return array(
        'id' => 'ID',
        'parametro_id' => 'Parametro',
    );
}

我试图制作复选框列表的表格

<div>
        <?php echo $form->checkboxList($model, $variable, array(0 => 1)); ?>
</div>
如果你问

,请输入

和文件内容

lololo trying

是自动生成的一些列,并且我将它们的名称存储在由空格分隔的此文件上,一切似乎都很好,直到我到达表单上的checkboxlist,它给出了错误,如strpos()或其他东西,如何我可以使这个复选框呈现并将1个数据保存到由特定列检查的那些人吗?

1 个答案:

答案 0 :(得分:1)

如果问题是你无法获得复选框,那么试试这个 的

更新

_form.php

 <div>
            <?php echo CHtml::checkBoxList('createCheck', array(), $variable); ?>
    </div>


您需要更改控制器操作,以便

public function actionCreate()
        {
            $model=new Parametro;

            $variable = file_get_contents('protected\column.txt');
            $vars = explode(' ', $variable);
          // make SURE that you are getting $vars as array
          if(isset($_POST['Parametro']))
            {
                $model->attributes=$_POST['Parametro'];
                if(isset($_POST['createCheck']))
            {
                 $newVar=array();
                $checkVariables=$_POST['createCheck'];
                foreach($checkVariables as $key)
                {
                    $newVar[]=$vars[$key];
                }
                if(!empty($newVar))
                {

                    foreach($newVar as $saveIt)
                    {

                        $model->$saveIt='y';
                    }
                     $model->save();
                }    

               }
               $this->redirect(array('view','id'=>$model->id));
            }

            $this->render('create',array(
                'model'=>$model,
                'variable'=>$vars,
            ));
        }

注意: - 它可能会给你一些错误,因为我不知道你的表结构。如果您遇到任何问题,请尝试解决或只是询问