ActiveForm checkboxList serialize checkbox array before save

Although I find this question addressed within the forum prior, having read the documentation and threads on the topic, after 1.5 days of trying I fail to solve the above following error on form submit ( of controller type actionUpdate ):

"Product Interest must be a string."

[where ‘Product Interest’ is a form checkboxList name=‘productInterest’ within my model ‘TblProspects’]

[ RESULT: Browser NOT redirected to the view.php, because $model->save() == FALSE ].

PERTINENT MODEL VALIDATION RULES IN frontend/models/TblProspects.php:

…[…‘productInterest’], ‘safe’],

… [[‘productInterest’], ‘string’, ‘max’ => 300],

beforeSave and afterFind methods in same model:




public function beforeSave($insert){

	//Our Array Model of the Database and Form Values for productInterest.


       // The structure for our checkboxlist options

	$list = [0=>'Ground Signs',1=>'Window Lettering',2=>'Channel Letter Signs',3=>'Wall Signs',4=>'Vehicle Lettering or Graphics',5=>'Banners',6=>'Digital Signs',7=>'Neon',8=>'Other'];        	

			

	$post = \Yii::$app->request->post('TblProspects');

	// POSTED runtime/log/file: array "TblProspects".... =>"productInterest";a:3:{i:0;s:1:"2";i:1;s:1:"4";i:2;s:1:"5";}

	// where selected checkbox values are options 2, 4, 5.			

		

	if(is_array($post['productInterest'])) {

		$checkedProducts = $post['productInterest'];

		$newArray = array();

		foreach($checkedProducts as $k=>$v){

		   array_push($newArray,$list[$v]); checkedProducts[0] = Vinyl Banner Signs				

		}

		$serialized = serialize($newArray);

                

                //! suspect the problem lies in the assignment to the post value;

		$post['TblProspects']['productInterest'] = $serialized;

		// Try also to assign it to the model's field in memory

		$this->productInterest = $serialed;

					

		$this->load($post);

		//When empty arguments, was getting error first parameter missing, so add $insert:

		parent::beforeSave($insert);

        } 

}//beforeSave




	public function afterFind()

	{

		    // convert serialized db fields into arrays

		    $this->productInterest = unserialize($this->productInterest);

		    return parent::afterFind();

		

	}//afterFind

			



And in my view’s update.php I echo the _updateform.php that contains the field for Product Interest as:





use yii\helpers\Html;

use yii\widgets\ActiveForm;


...


	$list = [0=>'Ground Signs',1=>'Window Lettering',2=>'Channel Letter Signs',3=>'Wall Signs',4=>'Vehicle Lettering or Graphics',5=>'Banners',6=>'Digital Signs',7=>'Neon',8=>'Other'];

	

	

	$checkedList = [];

	if(!$model->isNewRecord) {

		

		if( is_array( $model->productInterest) && count( $model->productInterest ) > 0 ){

			foreach( $model->productInterest as $k=>$v ){

				// save the index key value where the val is found in the checkbox names array.  These are the values

				// of the checkboxes that should be checked in the loaded form

				

				// array_search returns false, but may return other boolean (such as 0, which would equate to an arr index value)

				if( array_search($v, $list) !== FALSE ){

					array_push($checkedList, array_search($v, $list));

					$model->productInterest = $checkedList;

				}

			 }

		}

	}

	// var_dump($model->productInterest);

	// outputs (example): array(3) { [0]=> string(1) "2" [1]=> string(1) "4" [2]=> string(1) "5" }

	echo $form->field($model, 'productInterest')            

         ->checkboxList( $list,

         [

          'class'=>'chosen-select input-md',

          'multiple'=>'multiple'              

         ]             

        )->label();

   ...






Why is the attribute that I am trying to save violating the RULE of string, when I am serializing it using beforeSave?

Finally I was able to achieve success by using beforeValidate() instead of beforeSave(), as, it appears, the validation occurs before beforeSave (during the POSTing of the form data), and thus I was encountering the validation rule error that my POST[array] !== string.

SOLVED.