doubang9906
2017-04-04 11:05
浏览 48
已采纳

Codeigniter - 我的表单验证回调不起作用

I have tried to copy from Codegniter's documentation, but I can't make form validation callbacks working.

I added helper form, url and library form_validation. It's not working and always returns "false"

Controller

 public function addtest()
        {
            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');

            $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
            if($_SERVER['REQUEST_METHOD'] == 'POST')
            {
                 if ($this->form_validation->run() == TRUE)
                {
                       die('true');
                }
                else
                {
                       die('false');
                }
            }

            $this->template
                ->build('myform',array());
        }

    public function username_check($str)
    {
        if ($str == 'test')
    {
            return TRUE;
    }
    else
    {
            return FALSE;
    }
    }

View

<form method="post" action="" class="form-horizontal form-label-left">
    <div class="col-xs-12 col-md-9">
        <div class="x_panel">
            <div class="form-group col-xs-12">
                <div class="col-xs-3">
                    <label class="control-label">Folder name</label>
                </div>
                <div class="col-xs-9">
                    <input type="text" name="username" value="" class="form-control " id="" placeholder="">
                </div>
            </div>
            <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </div>
</form>

图片转代码服务由CSDN问答提供 功能建议

我试图从Codegniter的文档中复制,但我无法使表单验证回调起作用。

我添加了帮助表单,网址和库form_validation。 它不起作用并且始终返回“false”

控制器

  public function addtest()
 {
 $ this-&gt;  load-&gt; helper(array('form','url')); 
 
 $ this-&gt; load-&gt; library('form_validation'); 
 
 $ this-&gt; form_validation-&gt  ; set_rules('username','Username','callback_username_check'); 
 if($ _ SERVER ['REQUEST_METHOD'] =='POST')
 {
 if if($ this-&gt; form_validation-&gt; run)  ()== TRUE)
 {
 die('true'); 
} 
 else 
 {
 die('false'); 
} 
} 
 
 $ this-&gt  ; template 
  - &gt; build('myform',array()); 
} 
 
公共函数username_check($ str)
 {
 if if($ str =='test')
 {  
返回TRUE; 
} 
其他
 {
返回FALSE; 
} 
} 
   
 
 

查看 \ n

 &lt; form m  ethod =“post”action =“”class =“form-horizo​​ntal form-label-left”&gt; 
&lt; div class =“col-xs-12 col-md-9”&gt; 
&lt; div class  =“x_panel”&gt; 
&lt; div class =“form-group col-xs-12”&gt; 
&lt; div class =“col-xs-3”&gt; 
&lt; label class =“control  -label“&gt;文件夹名称&lt; / label&gt; 
&lt; / div&gt; 
&lt; div class =”col-xs-9“&gt; 
&lt; input type =”text“name =”username“value  =“”class =“form-control”id =“”placeholder =“”&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; button type =“submit”class =“btn btn- 成功“&gt;提交&lt; / button&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / form&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtnnpt11795 2017-04-04 11:11
    已采纳

    Extend your form_validation library in Libraries.php

    MY_Form_validation.php

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Form_validation extends CI_Form_validation
    {
        function run($module = '', $group = '') {
    
            (is_object($module)) AND $this->CI =& $module;
            return parent::run($group);
        }
    }
    /* End of file MY_Form_validation.php */
    /* Location: ./application/libraries/MY_Form_validation.php */ 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyuqie4322 2017-04-04 11:13

    My Controller function is like this and it runs perfectly. I have autoloaded all libraries

    public function change_password()
    {
        if($this->isLoggedin()){
            $data['title']='Change Password';
            if($_POST)
            {
                $config=array(
                    array(
                        'field' => 'old_password',
                        'label' => 'Old Password',
                        'rules' => 'trim|required|callback_checkPassword'
                    ),
                    array(
                        'field' => 'password',
                        'label' => 'Password',
                        'rules' => 'trim|required'
                    ),
                    array(
                        'field' => 'conf_password',
                        'label' => 'Confirm Password',
                        'rules' => 'trim|required|matches[password]'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false)
                {
                    // if validation has errors, save those errors in variable and send it to view
                    $data['errors'] = validation_errors();
                    $this->load->view('change_password',$data);
                }
                else
                {
                    // if validation passes, check for user credentials from database
                    $this->Login_model->updatePassword($_POST['password'],$this->session->userdata['id']);
                    $this->session->set_flashdata('log_success','Congratulations! Password Changed');
                    redirect(base_url() . 'Login/dashboard');
                }
    
            }
            else
            {
                $this->load->view('change_password',$data);
            }
        }
        else
        {
            redirect(base_url().'Login');
        }
    
    }
    
    public function checkPassword($str)
    {
        $check=$this->Login_model->checkPassword($str);
        if($check)
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('checkPassword', 'The Current Password you have provided is incorrect');
            return false;
        }
    }
    
    评论
    解决 无用
    打赏 举报
  • dqwh1202 2017-04-04 13:07

    In HTML (Add ID field)

    <input type="text" name="username" value="" class="form-control " id="username" placeholder="">
    
    <button type="submit" class="btn btn-success" id="submit">Submit</button>
    

    In your AJAX code

    <script type="text/javascript">
        $(function(){
            $( "#submit" ).click(function(event)
            {
                event.preventDefault();
                var username= $("#username").username();
    
                $.ajax(
                    {
                        type:"post",
                        url: "<?php echo base_url(); ?>index.php/controller/Method",
                        data:{ username:username},
                        success:function(res)
                        {
    
                        }
                    });
            });
        });
    </script>>
    

    In Controller

    No need of check if($_SERVER['REQUEST_METHOD'] == 'POST') cz its comes througu it always

    public function addtest()
    {
        $this->load->helper(array('form', 'url'));
    
        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
    
            if ($this->form_validation->run() == TRUE)
            {
                die('true');
            }
            else
            {
                die('false');
            }
    
        $this->template->build('myform',array());
    }
    
    
    public function username_check($str)
    {
        if (empty($str)) 
        {
            echo "Empty";
        } 
        else 
        {
            if ($str == 'test')
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }
    

    Check More about Codeigniter CallBack Functon

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题