yii,主动表单复选框列表



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

public function actionCreate()
    {
        $model=new Parametro;
        $variable = file_get_contents('protectedcolumn.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('protectedcolumn.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('protectedcolumn.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

是一些自动生成的列,并且将它们的名称存储在以空格分隔的文件中,在我到达表单上的复选框列表之前,一切似乎都很好,它给了我像 strpos() 之类的错误,我如何使这个复选框呈现并将 1 个数据保存到那些被特定列选中的人?

如果问题是您无法获取复选框,请尝试此操作

更新

在您的_form.php

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


您需要为此更改

控制器操作

public function actionCreate()
        {
            $model=new Parametro;
            $variable = file_get_contents('protectedcolumn.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,
            ));
        }

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

最新更新