我在yii中做了2次依赖下拉,工作正常。但是,即使从下拉列表中选择值,当我提交(创建)表单时,它显示如下错误: 请修正以下输入错误: Taluk不能空白。 区不能空白。
值不会插入数据库。我跟随了Easy solution for Dependent dropDownList Using AJAX和Creating a dependant dropdown
中给出的Everithing这是我在视图中的代码(_form.php):
<div class="row">
<?php
echo CHtml::dropDownList('district_id','',
array(4=>'Gulbarga',3=>'Bangalore Urban'),
array(
'prompt'=>'Select District',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadTaluk'),
'update'=>'#taluk_id',
'data'=>array('district_id'=>'js:this.value'),
)));
echo CHtml::dropDownList('taluk_id','', array(), array('prompt'=>'Select a Taluk'));
?>
</div>
在我的InquiriesController中:
public function actionLoadTaluk()
{
$data=Taluk::model()->findAll('district_id=:district_id',
array(':district_id'=>(int) $_POST['district_id']));
$data=CHtml::listData($data,'taluk_id','name');
echo "<option value=''>Select a Taluk</option>";
foreach($data as $value=>$name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
这是我的模型(Inquiries.php):
<?php
public function rules()
{
return array(
array('organization, aplication_type, contact_person, pan, city_town_village, taluk_id, district_id', 'required'),
array('organization, email', 'length', 'max'=>200),
array('aplication_type, contact_person, pan', 'length', 'max'=>100),
array('phone', 'length', 'max'=>60),
array('address', 'length', 'max'=>150),
array('pin_code', 'length', 'max'=>6),
array('city_town_village', 'length', 'max'=>128),
array('taluk_id, district_id', 'length', 'max'=>10),
array('inquiry_id, organization, aplication_type, contact_person, email, phone, pan, address, pin_code, city_town_village, taluk_id, district_id', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'taluk' => array(self::BELONGS_TO, 'Taluk', 'taluk_id'),
);
}
public function attributeLabels()
{
return array(
'inquiry_id' => 'Inquiry',
'organization' => 'Organization',
'aplication_type' => 'Aplication Type',
'contact_person' => 'Contact Person',
'email' => 'Email',
'phone' => 'Phone',
'pan' => 'Pan',
'address' => 'Address',
'pin_code' => 'Pin Code',
'city_town_village' => 'City Town Village',
'taluk_id' => 'Taluk',
'district_id' => 'District',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('inquiry_id',$this->inquiry_id,true);
$criteria->compare('organization',$this->organization,true);
$criteria->compare('aplication_type',$this->aplication_type,true);
$criteria->compare('contact_person',$this->contact_person,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('phone',$this->phone,true);
$criteria->compare('pan',$this->pan,true);
$criteria->compare('address',$this->address,true);
$criteria->compare('pin_code',$this->pin_code,true);
$criteria->compare('city_town_village',$this->city_town_village,true);
$criteria->compare('taluk_id',$this->taluk_id,true);
$criteria->compare('district_id',$this->district_id,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
有人帮帮我.. 提前谢谢。
答案 0 :(得分:0)
我的问题解决了。我想分享这个解决方案,因为它可能会帮助某人。 这是正确的视图代码。
<div class="row">
<?php
echo CHtml::dropDownList('Inquiries[district_id]','',
array(4=>'Gulbarga',3=>'Bangalore Urban',5=>'Bangalore Rural'),
array(
'prompt'=>'Select District',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadTaluk'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#Inquiries_taluk_id', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('district_id'=>'js:this.value'),
)));
echo CHtml::dropDownList('Inquiries[taluk_id]','', array(), array('prompt'=>'Select a Taluk'));
?>
</div>
以下是我在以下两行中所做的更改。
echo CHtml::dropDownList('Inquiries[district_id]','',
'update'=>'#Inquiries_taluk_id',