How To Validate Checkboxlist And Textfield On The Same Field

Hi,

I want to use a single field to collect input for both checkboxlist() and textfield().But how to display input errors (if any) in the same error. The error should not display when I fill the textfield ,but it doesn’t work.

eg:

controller:


 public function actionTest()

 {        

     $model=new FrameWorkForm();

     if(isset($_POST['FrameWorkForm'])){

         $model->attributes=$_POST['FrameWorkForm'];

         $model->validate();

     }

     $this->render('test',array('model'=>$model));

 }

"test.php" code:


<div style="margin:0 auto;width: 990px">

    <?php $form=$this->beginWidget('CActiveForm',array(

            'enableClientValidation'=>true,

            'clientOptions'=>array(

                'validateOnSubmit'=>true,

            )));?>

    <label style="font-weight:700">Framework:</label>

    <?php echo $form->checkboxList($model,'framework',array('Yii','Ci','Phalcon','Symfony2'),array('separator'=>'','uncheckValue'=>null));?>

    <label style="font-weight:700">other:</label>

    <?php echo $form->textField($model,'framework');?>

    <?php echo $form->error($model,'framework')?>

    <?php echo CHtml::submitButton('Create')?>

    <?php $this->endWidget();?>

</div>

"FrameWorkForm.php" code:


<?php


class FrameWorkForm extends CFormModel{

     

     public $id;

     public $framework;

     

     public function rules() {

         return array(

             array('framework','required'),

         );

     }

     

 }

Since your using a checkbox list and not select list im assuming you can check multiple.

You could merge them in a before validate in your model so the before validate is what gets validated. Just an idea and i don’t know if it’s the best way but it would work.




public function beforeValidate() {

		if(!$this->isNewRecord){

 		$this->framework =   imploding / merging them here;

		}

		return parent::beforeValidate();

	}



you could also assign a value to the other filed in your model and then use a custom validator




//in model


public $otherFramework;


public function requiredFramework($attribute,$params)

{

if (!isset($this->framework) or !isset($this->otherFramework)){

$this->addError('framework, 'Please select a framework or input one in the other field.');

}


public function rules()

{

return array(

array('framework', 'requiredFramework'),

);

}


//your going to want to merge them here if you allow multiple

public function beforeSave() {

  if(!$this->isNewRecord){

 $this->framework =   imploding / merge both $this->framework and $this->otherFramework;

  }

  return parent::beforeSave();

  }


//in form 


<?php echo $form->textField($model,'otherframework');?>



and welcome to the forum.

Thanks a lot. It works for me. But it’s a pity that it doesn’t work on clientValidateAttribute().

in jquery.yiiactiveform.js


	

       var getAFValue = function (o) {

 		var type,

			c = [];

		if (!o.length) {

			return undefined;

		}

		if (o[0].tagName.toLowerCase() === 'span') {

			o.find(':checked').each(function () {

				c.push(this.value);

			});

			return c.join(',');

		}

		type = o.attr('type');

		if (type === 'checkbox' || type === 'radio') {

			return o.filter(':checked').val();

		} else {

			return o.val();

		}

	};