Welcome Guest, Not a member yet? Register   Sign In
Form validation: Callback not changing the input value

[eluser]Nis Sarup[/eluser]
As I read the user guide any callback in a form validation rule should change the input value if the result returned from the callback function is anything but TRUE or FALSE.

I can't get it to do that.

I have a rule:
$rules['birthday'] = 'trim|required|callback__validate_birthday';
The callback is being called. It returns a timestamp if the birthday is valid, FALSE if not.

stays the same value whatever the callback returns. Shouldn't it be set to the result returned from the callback? Or did I misunderstand something?

Welcome to the CodeIgniter forums.

If the problem is with your callback, then it might be useful for you to post the code for it.

[eluser]Nis Sarup[/eluser]
Here is the callback:
function _validate_birthday()
            // Validates the birthdate from the user input.
            $day     = (int)$this->input->post('birthday');
            $month    = (int)$this->input->post('birthday_month');
            $year    = (int)$this->input->post('birthday_year');
            if (0 > $day || $day > 32) {
                $this->validation->set_message('validate_birthday', 'Invalid day in birthdate.');
                return false;
            if (0 > $month || $month > 13) {
                $this->validation->set_message('validate_birthday', 'Invalid month in birthdate.');
                return false;
            if (0 > $day || $day > (int)date('Y')) {
                $this->validation->set_message('validate_birthday', 'Invalid year in birthdate.');
                return false;
            $birthday = mktime(0, 0, 0, $month, $day, $year);
            if ($birthday > strtotime('-18 years')) {
                $this->validation->set_message('validate_birthday', 'You must be 18 years old to sign up.');
                return false;
            return $birthday;

It looks at the 3 inputs for the birthday, makes the timestamp and returns it if everything is allright.

Are you absolutely sure that everything is all right? I'd suggest sticking a die($birthday) in before the return. Then you can be sure that a) It's being reached, and b) the format is what you're expecting it to be.

[eluser]Nis Sarup[/eluser]
I did. That's how I know it's being called.
I stick die's all over the code when I can't get it to work.

It returns a a unixtimestamp from the mktime() function.
The rest of the validation works as well. It triggers a FALSE if I set my birthday to something less than 18 years ago.

I even tried to make the calback return 'Some string" and the input was still unchanged.

I'm not entirely sure what's happening in that case, since it's always worked as expected for me. One problem I have noticed though, which is totally unrelated, is that the first parameter for $this->form_validation->set_message() should be '_validate_birthday' and not 'validate_birthday'.

Oh, and another thing. You're validating the day against the current year. Surely that's not right?
if (0 > $day || $day > (int)date('Y')) {
    $this->validation->set_message('validate_birthday', 'Invalid year in birthdate.');
    return false;

[eluser]Nis Sarup[/eluser]
Yeah. I had originally named the function without the starting underscore, but read that that was a bad thing to do.

I just corrected those set_message's but it doesn't change anything.

Here is the signup function that runs the validation:
function signup()
            $this->load->library(array('validation', 'email'));
            $rules['username']                = 'trim|required|min_length[5]|max_length[50]|callback__username_check|xss_clean|htmlentities';
            $rules['password']                = 'trim|required|matches[password_2]';
            $rules['password_2']            = 'trim|required';
            $rules['email']                     = 'trim|required|matches[email_2]|valid_email|callback__email_check';
            $rules['email_2']                = 'trim|required';
            $rules['first_name']            = 'trim|required|xss_clean';
            $rules['last_name']                = 'trim|required|xss_clean';
            $rules['state']                    = 'trim|required|exact_length[2]|alpha';
            $rules['street']                = 'trim|required';
            $rules['zip']                    = 'trim|required';
            $rules['birthday_month']        = 'trim|required';
            $rules['birthday_year']            = 'trim|required';
            $rules['birthday']                = 'trim|required|callback__validate_birthday';
            $fields['username']                = 'Username';
            $fields['password']                = 'Password';
            $fields['password_2']            = 'Repeated password';
            $fields['email']                 = 'Email';
            $fields['email_2']                = 'Repeated email';
            $fields['first_name']            = 'First name';
            $fields['middle_name']            = 'Middle name';
            $fields['last_name']            = 'Last name';
            $fields['phone']                = 'Phone';
            $fields['birthday_month']        = 'Birthmonth';
            $fields['birthday']                = 'Birthday';
            $fields['birthday_year']        = 'Birthyear';
            $fields['state']                = 'State';
            $fields['street']                = 'Street';
            $fields['street_2']                = 'Street 2';
            $fields['zip']                    = 'Zip';
            $data = array('title' => 'Sign up', 'where' => array('sale-rent' => false, 'buyer-renter' => false, 'forum' => false, 'blogs' => false));
            if ($this->validation->run() === false) {
                // Show the signup form
                // Either because it's the users first request
                // or because validation wasn't passed.
                $data['content'] = $this->load->view('user/signup', false, true);
            } else {
                // Success! Save the user, send an email, profit!
                $register = $this->redux_auth->register($this->input->post('username'), $this->input->post('password'), $this->input->post('email'));
                $data['content'] = $this->load->view('user/signup-success', false, true);
            $this->load->view('main_template', $data);
Maybe I missed something there.
I left in the die() for you. It outputs whatever I selected in the dropdown in the form. Not what the callback returns.

[eluser]Nis Sarup[/eluser]
[quote author="TheFuzzy0ne" date="1246552704"]Oh, and another thing. You're validating the day against the current year. Surely that's not right?[/quote]

Hehe, you're right, those $day's should be $year's. Thanks.

I'm sorry, but unfortunately I'm about to go out for a few hours, so this will have to wait. If you're still stuck when I get back, I'll test the code, and see if I can get to the bottom of this.

Theme © iAndrew 2016 - Forum software by © MyBB