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