所以我是Yii新手,我正和朋友一起组建一个网站。我已经掌握了基础知识,但我遇到了一个错误。我想要一个带有两个下拉菜单的页面。第二个下拉菜单的数据基于第一个下拉菜单中的信息。我希望能够验证这两个下拉菜单都选择了可行的数据,这就是问题所在。
每当我尝试验证下拉菜单时,它总是说它们是空的,是否已经选择了可行的选项。我认为这是一件我没看到的小事,但我会喜欢一些帮助。提前谢谢!
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'),
);
}
答案 0 :(得分:0)
我认为你应该添加这段代码:
$model->attributes=$_POST['Room'];
前
echo($model->validate());
希望它有效,如果我错了,请纠正我.. :)