duanlianyun0462 2014-07-09 12:22
浏览 7
已采纳

Yii中的相关下拉错误不能为空

I have made 2 dependant dropdown in yii which works fine. But, even after selecting values from dropdown list, when i submit(create) form, Its showing below error : Please fix the following input errors: Taluk cannot be blank. District cannot be blank .

And values are not inserting into the database. I have followed Everithing given in Easy solution for Dependent dropDownList Using AJAX and Creating a dependant dropdown

Here is my code in view(_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>

in my 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);
    }

Here is my Model(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);
    }
}

Somebody Help me Please.. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongyuan7110 2014-07-17 06:33
    关注

    My issue solved. I want share this solution as it may help somebody. Here is correct code for view.

    <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>
    

    Here is the changes i have made in following two lines.

    echo CHtml::dropDownList('Inquiries[district_id]','', 
    
    'update'=>'#Inquiries_taluk_id',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名