0

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>
10
  • 1
    Try to add $this in $this->form_validation->run($this); Commented Apr 4, 2017 at 11:07
  • I have added $this but it return false again Commented Apr 4, 2017 at 11:08
  • can you try by removing request method check ... ? Commented Apr 4, 2017 at 11:10
  • 1
    try callback_username_check[username] and codeigniter.com/user_guide/libraries/… Commented Apr 4, 2017 at 11:11
  • If i remove request mothod, i always run code "die('false')" Commented Apr 4, 2017 at 11:12

3 Answers 3

1

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 */ 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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') because it comes through 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 function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.