Welcome Guest, Not a member yet? Register   Sign In
Pulling my hair out implementing form validation
#1

[eluser]CI Evan[/eluser]
Seems the form validation class won't execute any callbacks or formatting functions unless the field is required.

Code:
// controller
    function save()
    {
        $rules = array(
            array(
                'field' => 'effective',
                'label' => 'Effective Date',
                'rules' => 'trim|required|callback__valid_date|to_timestamp'
            ),
            array(
                'field' => 'expiration',
                'label' => 'Expiration Date',
                'rules' => 'trim|callback__valid_date_or_blank|to_timestamp'
            )
        );
        // to_timestamp($str) is defined in an autoloaded helper

        
        $this->form_validation->set_rules($rules);
        $this->form_validation->set_message('_valid_date', 'The %s field is invalid.  Use MM/DD/YYYY.');
        $this->form_validation->set_message('_valid_date_or_blank', 'The %s field is invalid.  Use MM/DD/YYYY.');        
        
        $action = ($this->input->post('id') == 0) ? 'create' : 'edit';
        
        if($this->form_validation->run())
        {
            print_r($_POST);
        }
        else
        {
            echo 'failed';
        }
    }
    
    function _valid_date($date){ return is_valid_date($date); }
    function _valid_date_or_blank($date){ return (strlen($date) > 0) ? is_valid_date($date) : TRUE; }

// autoloaded helper
function to_timestamp($date)
{
    if(!(strlen($date) > 0)) return $date;
    
    $pieces = explode('/', $date);
    $human = sprintf('%3$s-%1$s-%2$s 0:0:0 AM', $pieces[0], $pieces[1], $pieces[2]);
    return human_to_unix($human);
}

function is_valid_date($date)
{
    // format check
    $reg_ex = "@(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d{2}@";
    if((preg_match($reg_ex, $date) != 1) OR (strlen($date) != 10)) return FALSE;

    $pieces = explode('/', $date);
    
    // semantic check (do not allow 02/31/2009)
    if(!checkdate($pieces[0], $pieces[1], $pieces[2])) return FALSE;
    
    return TRUE;
}

With valid input for both fields, $_POST['effective'] is processed and outputted as a numeric timestamp, where as $_POST['expiration'] is not processed at all and instead outputted as the string originally entered (12/31/2009).

What am I doing wrong?
#2

[eluser]ggoforth[/eluser]
don't think the $_POST array is actually manipulated. Try using set_value('field_name') instead of the post array. That or $this->input->post('field_name'). Give that a shot and see if it works.
#3

[eluser]Colin Williams[/eluser]
Quote:Seems the form validation class won’t execute any callbacks or formatting functions unless the field is required.

This was fixed in the latest version.
#4

[eluser]CI Evan[/eluser]
@ggoforth: $_POST is manipulated, or the value for $_POST['effective'] wouldn't be affected. set_value() and $this->input->post() both give the same result anyway.

@Colin Williams: Just downloaded the latest off of the front page and got the same result. Did you mean svn trunk?
#5

[eluser]CI Evan[/eluser]
@Colin Williams: svn trunk gives same result as well.
#6

[eluser]VernonK[/eluser]
This might be out of left field and only because it's Friday, but... Do you think the extra underscore (e.g _valid_date -> callback__valid_date) is throwing off CI? CI is looking for something like: callback_valid_date. I know it sounds crazy, but it would only take a minute to check.
#7

[eluser]CI Evan[/eluser]
@VernonK: tried your suggestion, same result.
#8

[eluser]Michael Wales[/eluser]
Quote:This might be out of left field and only because it’s Friday, but… Do you think the extra underscore (e.g _valid_date -> callback__valid_date) is throwing off CI? CI is looking for something like: callback_valid_date. I know it sounds crazy, but it would only take a minute to check.

No, that is actually the preferred way to do it. otherwise, people would be able to access those functions via the URL.
#9

[eluser]VernonK[/eluser]
Oh right... duh... Like I said... out of left field.
#10

[eluser]CI Evan[/eluser]
@michael Wales: Aware you have extensive knowledge of the framework, any ideas as to why this is not working or potential work arounds?




Theme © iAndrew 2016 - Forum software by © MyBB