validation - 验证 CodeIgniter 中的组合框

标签 validation combobox codeigniter-3

我正在使用 CodeIgniter 3,试图验证我的两个组合框。

我的代码是这样的:

查看:

<tr>
     <td >
        <?php $extra=' class="selectCombo"';
            $busVal=array();
            $busVal[]="Select";
            foreach($values as $val){
                $busVal[$val->bus_id]=$val->bus_no;
            }
        echo form_dropdown('bus_id', $busVal,set_value('bus_id'),$extra);
        ?> 
       </td>
       <td><?php echo form_error('bus_id');?></td>
</tr>
<tr>
     <td >
        <?php
            $extra=' class="selectCombo"';
            $route=array();
            $route[]="Select";
            foreach($values as $val){
                $route[$val->route_id]=$val->route_name;
            }
            echo form_dropdown('route_id',$route,set_value('route_id'),$extra);
            ?> 
           </td>
           <td><?php echo form_error('route_id');?></td>
</tr>

我的 Controller 代码是这样的:

public function add()
{
$this->form_validation->set_rules('bus_id', 'Bus', 'trim|required|callback_check_Bus');
$this->form_validation->set_rules('route_id', 'Route Name', 'trim|required|callback_validate_route');
if ($this->form_validation->run() == FALSE) {
    echo 'Error';
}else{
  }
}
public function check_Bus($val){
        if($val ==0)
        {
          $this->form_validation->set_message('select_validate', 'Please Select Bus.');
          return false;
        } 
        else
        {
          return true;
         }
    }

     public function validate_route($val){
        if($val ==0)
        {
          $this->form_validation->set_message('select_validate', 'Please Select Route.');
          return false;
        } 
        else
        {
          return true;
         }
    }

我收到以下两个字段的消息:

Unable to access an error message corresponding to your field name Bus.(check_Bus)
Unable to access an error message corresponding to your field name Route Name.(validate_route)

我应该做什么?

最佳答案

使用此代码工作正常。

callback validation rule
callback_fieldname_check        
//function name callback_fieldname_check (){

}

// Set message - fieldname_check

查看代码

public function add()
{

        $this->form_validation->set_rules('bus_id', 'Bus', 'trim|required|callback_bus_id_check');
        $this->form_validation->set_rules('route_id', 'Route Name', 'trim|required|callback_route_id_check');
        if ($this->form_validation->run() == FALSE) {
        $this->load->view('load_your_view');
        }else{
        echo "Added";
        }
}
public function bus_id_check($val){
        if($val ==0)
        {
          $this->form_validation->set_message('bus_id_check', 'Please Select Bus.');
          return false;
        } 
        else
        {
          return true;
         }
    }

     public function route_id_check($val){
        if($val ==0)
        {
          $this->form_validation->set_message('route_id_check', 'Please Select Route.');
          return false;
        } 
        else
        {
          return true;
         }
    }

关于validation - 验证 CodeIgniter 中的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871807/

相关文章:

jsf - 两个 primefaces 日历组件验证

validation - jaxb 验证事件定位器 - 验证错误的行数和列数

javascript - Cakephp 2.x - 隐藏/显示组合框

php - 使用 condition/codeigniter 将数据插入数据库

php - iframe colorbox 联系表单不会提交,样式不会在验证后保留

php - 使用 php 检测 JSON 中的重复键

java - ExtGWT ComboBox 下拉列表中的多行项目

json - 获取 JSON 数组并将项目添加到 Delphi 中的 Combobox

mysql - 一组行的 ORDER BY 条件

php - CI_DB_mysqli_result 类的对象无法转换为字符串?