So I'm a Yii Newbie, and I'm putting together a website with a friend. I've got the basics down, but I'm running into an error. I want to have a page with two drop-down menus. The second drop down menu's data is based on information from the first drop down menu. I want to be able to verify that both of these drop down menu's have selected viable data, and herein lies the problem.
Whenever I try to verify the drop down menus, it always says that they are empty, whether they have selected viable options or not. I assume it's a small thing I'm not seeing, but I'd love some help. Thanks in advance!
public function actionResTest()
{
$model = new Room;
if(isset($_POST['yt0']))
{
echo($model->validate());
}
$this->render('resTest',array('model'=>$model));
if(!isset($_POST['Room']['building_id']))
{print_r($_POST); $_POST['Room']['building_id'] = '';}
$data = Room::model()->findAll('building_id=:building_id',
array(':building_id'=>(int) $_POST['Room']['building_id']));
$data = CHtml::listData($data,'room_id','room_number');
echo CHtml::tag('option', array('value'=>''), 'Choose a room', true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
<div class="form">
<?php
$build = $this -> getBuildings();
$buildList = CHtml::listData($build, 'building_id', 'name');
?>
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'room-resTest-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form -> labelEx($model, 'building_id');
echo $form -> dropDownList($model, 'building_id',$buildList,
array(
'empty'=> 'Choose a building',
'ajax' => array(
'type' => 'POST',
'url'=> CController::createUrl('room/resTest'),
'update'=>'#' . CHtml::activeId($model, 'room_number'),
)));
echo $form -> error($model, 'building_id'); ?>
</div>
<div class="row">
<?php
echo $form -> labelEx($model, 'room_number');
echo $form -> dropDownList($model,'room_number',array('empty'=>'select a building'));
echo $form -> error($model, 'room_number');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('room_number, building_id', 'required'),
array('room_number, building_id', 'numerical', 'integerOnly'=>true),
array('description', 'length', 'max'=>200),
array('image_url', 'length', 'max'=>150),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('room_id, room_number, building_id, description, image_url', 'safe', 'on'=>'search'),
);
}
I think it you should add this code:
$model->attributes=$_POST['Room'];
before
echo($model->validate());
Hope it works, correct me if i'm wrong.. :)