phpvalidationyiicheckboxlistyii-cformmodel

yii, CactiveForm checkboxlist


im really trying to render a checkboxlist, never did make a try on it before so im having problems with it, im trying to render all the checkboxes foreach column or attribute made, this is the action controller:

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,
        ));
    }

this is the model with another some methods

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',
    );
}

the form where im trying to make the checkboxlist

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

and the content of the file if you ask

lololo trying

the are some columns that are being generated automatically and im storing their names on this file separated by spaces, everything seems to be fine until i got to the checkboxlist on form, it gives me error like strpos() or something, how can i make this checkbox to render and to save 1 data to thoses who are checked by specific columns?


Solution

  • If the problem is that you are no able to get the check boxes then try this

    Update

    In your _form.php

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


    you need to change your controller action for that

    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,
                ));
            }
    

    Note:- it might throw you some errors as i do not know your table structure. If you get any problem try to solve it or just ask