post not happening

I have an extra checbox in my view file. This is not a field name but it is defined in the model file like this:


public $period;

This is used in view file like this:





        <?= $form->field($model, 'corr_id')->dropDownList($this->params['itemCorridors'],[ 'prompt' => '--- choose from ---' ]) ?>

        <?= $form->field($model, 'stn_id')->dropDownList($this->params['itemStationCodes'],[ 'prompt' => '--- choose from ---' ]) ?>

        <?= $form->field($model,'period')->checkbox()  ?>

[size=2]

[/size]

[size=“2”]In the controller file, I try to load the posted values. The other model posted variables are loaded but the check box value doesn’t get loaded:[/size]

[size="2"]


[/size]


public function actionStationFault()

    {

        $model = new Breakdown();


        $itemCorridors = ArrayHelper::map(Corridor::find()->all(), 'corr_id', 'corr_name');

        $itemStationCodes = ArrayHelper::map(StationCode::find()->orderBy('stn_id DESC')->all(), 'stn_id', 'station_name');


        $this->view->params['itemCorridors'] = $itemCorridors;

        $this->view->params['itemStationCodes'] = $itemStationCodes;


        $stationSelected = '';

        $lfailure = '';

        $efailure = '';

        $period = '';


        if ( $model->load(Yii::$app->request->post())) {

        

            

            $stn_id = '00'.$model['stn_id'];

            $corr_id = '0'.$model['corr_id'];

            $period = $model['period'];


            //$period = Yii::$app->request->post()['period'];


            $stationSelected = (string) StationCode::findOne($stn_id)['station_name'];

            $lfailure = Breakdown::find()

            ->select(['DATE_FORMAT(FROM_UNIXTIME(reporting_time),  "%b-%Y") AS MONTH', 'COUNT(1) AS FAIL' ])

            ->where(['SUBSTRING(`asset_code`,9,2)' => '18', 'SUBSTRING(`asset_code`,3,4)' => $stn_id, 'resp_contractor' => '1'])

            ->groupBy(['DATE_FORMAT( FROM_UNIXTIME( reporting_time ) ,  "%b-%Y")'])

            ->orderBy(['reporting_time' => SORT_ASC])

            ->all();


            $efailure = Breakdown::find()

            ->select(['DATE_FORMAT(FROM_UNIXTIME(reporting_time),  "%b-%Y") AS MONTH', 'COUNT(1) AS FAIL' ])

            ->where(['SUBSTRING(`asset_code`,9,2)' => '17', 'SUBSTRING(`asset_code`,3,4)' => $stn_id, 'resp_contractor' => '1'])

            ->groupBy(['DATE_FORMAT( FROM_UNIXTIME( reporting_time ) ,  "%b-%Y")'])

            ->orderBy(['reporting_time' => SORT_ASC])

            ->all();

        }




        return $this->render('station-fault', [

                    'model'=> $model, 

                    'itemCorridors'=> $itemCorridors, 

                    'itemStationCodes'=> $itemStationCodes,

                    'stationSelected'=> $stationSelected,

                    'period'=> $period,

                    'lfailure'=> $lfailure,

                    'efailure'=> $efailure,

                ]);

    }

[size=2]

[/size]

If I look at the request, then I can see that the checkbox value is available in $_POST:




[        'corr_id' => ''        'stn_id' => '14'        'period' => '1'    ]



Why is the checkbox value not getting loaded, where am I going wrong?

Have you marked your field as “safe” in the model’s rules?

Thanks. marking it as ‘safe’ solved the problem!

You should never use the safe validation rule for attributes that accept user input as they will not be validated. You should assign a proper validation rule to it. The reason it’s not being loaded is because you don’t have a validation rule so yii wont assign it to help reduce attacks however by making it safe you void this security measure.