简体   繁体   English

Yii验证下拉变化

[英]Yii validate dropdown onchange

I am trying to validate the dropdownlist as the value of dropdownlist changes. 我正在尝试验证dropdownlist的值,因为dropdownlist的值会发生变化。 I want to check is there an in the the table already of the selected job status. 我想检查表格中是否已存在所选作业状态。 Below is my code: 下面是我的代码:

<script>
     function validate_dropdown(id)
    {
        alert("Selected id = "+id);
        //var msg = <?php echo NotificationRules::model()->validate_job($_POST['id']);?>
        //alert("Message from model func = "+msg);
    }
</script>

<?php
  echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
            array('empty'=>'Please Select job status (required)', 'onchange'=>'js:validate_dropdown(this.value)')
        );


?>

I am trying to pass js variable id to php function and send back a message if there is already an entry for the selected job status. 我正在尝试将js变量id传递给php函数,并发送一条消息,如果已存在所选工作状态的条目。 I am able to get selected value in js function validate_dropdown(), but not able to proceed further.Anybody pls help....... 我能够在js函数validate_dropdown()中获得选定的值,但无法继续进行操作。

Check this bellow example. 请查看以下示例。 In this i'm displaying all the users in a drop down list. 在此,我将在下拉列表中显示所有用户。 I'm keeping user id as option value and username as option label . 我将用户ID保留为选项值 ,将用户名保留为选项标签

User Table: 用户表:

    id  username    
    ------------------
    1   Heraman
    2   Dileep  
    3   Rakesh
    4   Kumar   


    <?php
    $list=CHtml::listData(User::model()->findAll(), 'id', 'username');       
    echo CHtml::dropDownList('username', $models->username, $list, array('empty' => '---Select User---','onchange'=>'alert(this.value)'));
    ?>

In you case, you can use 在这种情况下,您可以使用

 'onchange'=>'validate_dropdown(this.value)

  //Your script
  <script>
        function validate_dropdown(id)
        {
            alert("Selected id = "+id);        
        }
    </script>

I solved the problem by making an AJAX call... 我通过调用AJAX来解决了这个问题。

Final working code: 最终工作代码:

Code of dropdown in view 视图中的下拉代码

<?php
   echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
             array(//AJAX CALL.
                    'prompt' => 'Please Select job status (required)',
                    'value' => '0',
                    'ajax'  => array(
                            'type'  => 'POST',
                            'url' => CController::createUrl('NotificationRules/notificationPresent/'),
                            'data' => array("job_id" => "js:this.value"),
                            'success'=> 'function(data) {
                                    if(data == 1)
                                    {
                                        alert("Rule is already present for this status, Please update existing rule.");
                                    }
                                }',
                            'error'=> 'function(){alert("AJAX call error..!!!!!!!!!!");}',
                    )//end of ajax array().
        ));

?>

Code in controller(action) 控制器中的代码(动作)

<?php
   public function actionNotificationPresent()
   {
    if (Yii::app()->request->isAjaxRequest)
    {
        //pick off the parameter value
        $job_id = Yii::app()->request->getParam( 'job_id' );
        if($job_id != '')
        {
            //echo "Id is received is ".$job_id;

            $rulesModel = NotificationRules::model()->findAllByAttributes(array('job_status_id'=>$job_id));

            if(count($rulesModel))
                echo 1;
            else 
                echo 0;

        }//end of if(job_id)
        else
        {
            //echo "Id id not received";
            echo 0;

        }

    }//end of if(AjaxRequest).

}//end of NotificationPresent().

?>

Now i am getting an alert if there is any rule already with selected job status. 现在,如果有任何与所选工作状态有关的规则,我将收到警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM