• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation and callbacks

#1
[eluser]kilishan[/eluser]
Hey gang, Working on an issue with Bonfire and it seems that the form_validation callbacks aren't working any more. I could have sworn I had these working before, but now they don't even appear to be called. Am I missing something here?

Code:
if ($this->input->post('submit'))
        {
            // Validate input
            //$this->form_validation->set_rules('email', 'Email', 'required|trim|callback_unique_email|strip_tags|valid_email|max_length[120]|xsx_clean');
            $this->form_validation->set_rules('email', 'Email', 'callback_unique_email');
            if (config_item('auth.use_usernames'))
            {
                $this->form_validation->set_rules('username', 'Username', 'required|trim|strip_tags|max_length[30]|callback_unique_username|xsx_clean');
            }
            $this->form_validation->set_rules('password', 'Password', 'required|trim|strip_tags|min_length[8]|max_length[120]|xsx_clean');
            $this->form_validation->set_rules('pass_confirm', 'Password (again)', 'required|trim|strip_tags|matches[password]');
            
            if ($this->form_validation->run() !== false)
            {
                // Time to save the user...
                $data = array(
                    'email'        => $_POST['email'],
                    'username'    => isset($_POST['username']) ? $_POST['username'] : '',
                    'password'    => $_POST['password']
                );

                if ($this->user_model->insert($data))
                {
                    redirect('login');
                }
            }
        }
    
        Template::set_view('users/users/register');
        Template::set('page_title', 'Register');
        Template::render();
    }
    
    //--------------------------------------------------------------------
    
    public function unique_email($email)
    {
        if ($this->user_model->is_unique('email', $email) === true)
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('unique_email', 'That email address is already in use.');
            return false;
        }
    }
    
    //--------------------------------------------------------------------

#2
[eluser]InsiteFX[/eluser]
You should not be using $_POST but $this->input->post('email', TRUE);

The call_back should be the last rule on the validation rules with nothing after it.

You can also make the call_backs private by doing this
Code:
// add an extra underscore to the call_back
$this->form_validation->set_rules('email', 'Email', 'callback__unique_email');

// add a underscore to the beginning of the call_back function
public function _unique_email($email)
    {
        if ($this->user_model->is_unique('email', $email) === true)
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('unique_email', 'That email address is already in use.');
            return false;
        }
    }
If your using HMVC see the documentation on form_validation.

InsiteFX

#3
[eluser]kilishan[/eluser]
Nice tip about making it private. Thanks!

As for $this->input->post vs $_POST, it's my understanding the only thing the helper function does is return false if not set, correct?

On the email validation rule, the callback is the only rule in there, and it's still not making it to the actual callback function.

I am running HMVC, but I've already assigned the CI pointer to $this in MY_Controller. I'm pretty sure, anyway. I'll have to double-check.

#4
[eluser]InsiteFX[/eluser]
Quote:As for $this->input->post vs $_POST, it’s my understanding the only thing the helper function does is return false if not set, correct?

No: $this->input->post will return the same thing.

CodeIgniter User Guide - Input Class

As for HMVC I would double check that, that sounds like where your problem is.
You may need to add the $this to you code above.

InsiteFX

#5
[eluser]kilishan[/eluser]
Yup. It was the HMVC issue.

Thanks for the help in kicking the dust out of my brain.

#6
[eluser]InsiteFX[/eluser]
LOL! No problem, glad you got it working!

InsiteFX


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.