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

[eluser]bhogg[/eluser]
It's CI 1.7.1, no changes to CI_Form_validation class made...
#22

[eluser]garymardell[/eluser]
Try only using two == in the ...validation->run() == false line.
#23

[eluser]bhogg[/eluser]
Same effect with type sensitive or type insensitive comparison....
#24

[eluser]coolgeek[/eluser]
I've got a problem trying to extend the form validation library

Code:
<?php

class Add_member_award extends Controller {

    function Add_member_award()
    {
        parent::Controller();
    }

    function index() {


        $data['mview'] = "add_member_award";

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

        $this->form_validation->set_rules('award', 'Award', 'trim|required|xss_clean|callback_award_check');

        if ($this->input->post('submit')){
            if ($this->form_validation->run() == TRUE) {
                $award = $this->input->post('award');

                $this->load->model('Mmember_award');
                $this->Mmember_award->createMemberAward($uid, $award);
                redirect("member/$uid");
            }
        }
        $this->load->vars($data);
        $this->load->view('template', $data);
    }
}

class MY_Form_validation extends CI_Form_validation {

    function My_Form_validation()
    {
        parent::CI_Form_validation();
    }


    function award_check($str)
    {
        if ($str == 'test award')
        {
            $this->form_validation->set_message('award_check', 'The %s field can not be the word "test award"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

If I put function award_check in the controller, form validation triggers the check as expected. But it's not triggering it in MY_Form_validation.

What am I doing wrong here?

Thanks
#25

[eluser]TheFuzzy0ne[/eluser]
I trust that the MY_Form_validation class defined in ./system/application/libraries/MY_Form_validation.php, and not in the same file as your controller?
#26

[eluser]coolgeek[/eluser]
Quote:I trust that the MY_Form_validation class defined in ./system/application/libraries/MY_Form_validation.php, and not in the same file as your controller?

ugh, that was part of the problem - I had it saved as ./system/application/libraries/My_Form_validation.php (lower case 'y') with a similarly named constructor (though the class name was correct)

The extension is now being recognized (I know this because a PHP error got flagged in another function that I edited out of my post for brevity), but it's still not triggering callback_award_check when the award field is 'test award'

and thanks for the reply
#27

[eluser]TheFuzzy0ne[/eluser]
If the callback is definitely not being triggered at all, the only thing that comes to mind for me, is that the corresponding field is not being submited when you submit the form. Have you tried a print_r() of $_POST to see what's in there?
#28

[eluser]coolgeek[/eluser]
That's not it... you'll note that my controller calls a model to insert a record and the record does in fact insert, with a value of 'test award'.

In fact, this silly callback function was created in order to try to debug my real problem, which is with form_dropdown. I have another callback function:

Code:
function date_check () {
    if (isset($_POST['month']) && isset($_POST['day']) && isset($_POST['year'])
        && $_POST['month'] != 0 && $_POST['day'] != 0 && $_POST['year'] != 0) {
        if (checkdate($_POST['month'], $_POST['day'], $_POST['year']))
            return TRUE;
        else
        {
            $this->form_validation->set_message('date_check', 'The date field is invalid');
            return FALSE;
        }
    }
    else
    {
        $this->form_validation->set_message('date_check', 'The date field is invalid');
        return FALSE;
    }
}

that I'm calling as such (in the controller)

Code:
$this->form_validation->set_rules('year', 'Year', 'trim|required|xss_clean|callback_date_check');

I'm setting the form_input field to 'test award', and not setting the form_dropdown fields at all. When I click the submit button, not only is the record being created with the name 'test award', but the date is being set to '0000-0-0'

For all intents and purposes, the callback_award_check was copied directly from the Form Validation doc page, which is why I find this so frustrating

EDIT: fwiw, here is the generated html

Code:
<div id="award" class="inputformtext"><p>Award:&lt;input type="text" name="award" value="" size=""  /&gt;
#29

[eluser]TheFuzzy0ne[/eluser]
How about this:
Code:
function date_check ()
{
    $day = $this->input->post('day');
    $month = $this->input->post('month');
    $year = $this->input->post('year');

    if (checkdate($month, $day, $year))
    {
        return TRUE;
    }

    $this->form_validation->set_message('date_check', 'The date field is invalid');

    return FALSE;
}

It should work in the same way.

NB: Just an observation - I noticed that the month is passed to checkdate() as the first parameter, and I expected it to pass the day in as the first parameter, is that right?
#30

[eluser]coolgeek[/eluser]
Still generating a record with "test award" as the name and '0000-0-0' as the date. Here's the updated (and complete) copy of the validation library

Code:
&lt;?php
class MY_Form_validation extends CI_Form_validation {

    function MY_Form_validation()
    {
        parent::CI_Form_validation();
    }

    function date_check ($str) {

        $day = $this->input->post('day');
        $month = $this->input->post('month');
        $year = $this->input->post('year');

        if (checkdate($month, $day, $year))
            return TRUE;
        else
        {
            $this->form_validation->set_message('date_check', 'The date field is invalid');
            return FALSE;
        }
    }

    function award_check($str)
    {
        if ($str == 'test award')
        {
            $this->form_validation->set_message('award_check', 'The %s field can not be the word "test award"');
            return FALSE;
        }
        else
        {
            //return TRUE;
            return 'wtf';
        }
    }
}

You'll note that I'm even trying to change the value in award_check in case the if failed for some bizarre reason. Also note that I've updated to accept a parameter in date_check, even though I'm not using it.

I am continuing to get (and fix) PHP errors flagged on MY_Form_validation.php when I've got a typo, so I am confident that CI_Form_validation is being extended

and yes, checkdate takes month first

http://us3.php.net/manual/en/function.checkdate.php




Theme © iAndrew 2016 - Forum software by © MyBB