Part of the EllisLab Network
   
 
Pulling my hair out implementing form validation
Posted: 25 June 2009 01:45 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

Seems the form validation class won’t execute any callbacks or formatting functions unless the field is required.

// 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?

Profile
 
 
Posted: 25 June 2009 04:31 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  156
Joined  01-21-2008

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.

 Signature 

greg goforth
http://www.pencilem.com

Profile
 
 
Posted: 25 June 2009 06:38 PM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

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.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 26 June 2009 09:29 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

@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?

Profile
 
 
Posted: 26 June 2009 09:40 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

@Colin Williams: svn trunk gives same result as well.

Profile
 
 
Posted: 26 June 2009 10:23 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  10
Joined  09-11-2008

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.

Profile
 
 
Posted: 26 June 2009 10:46 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

@VernonK: tried your suggestion, same result.

Profile
 
 
Posted: 26 June 2009 10:47 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

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.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 26 June 2009 10:51 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Avatar
Total Posts:  10
Joined  09-11-2008

Oh right… duh… Like I said… out of left field.

Profile
 
 
Posted: 26 June 2009 11:26 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

@Michael Wales: Aware you have extensive knowledge of the framework, any ideas as to why this is not working or potential work arounds?

Profile
 
 
Posted: 26 June 2009 05:13 PM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

Thought I had heard murmurs that this was fixed in 1.7.1. Sorry for the misdirection.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 30 June 2009 11:39 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  9
Joined  04-02-2006

*bump* Anyone..?

Profile
 
 
Posted: 30 June 2009 12:40 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  88
Joined  01-08-2009

I think this is a bug.

Try commenting out the following in the Form_validation.php file.

if ( ! in_array('required'$rulesTRUE) AND $result !== FALSE)
{
    
return;

(it’s around line 670)

 Signature 

Web Design Essex - Essex SEO - Essex Alloy Wheel Repair

Profile
 
 
Posted: 01 July 2009 03:56 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  88
Joined  01-08-2009

How did you get on?

 Signature 

Web Design Essex - Essex SEO - Essex Alloy Wheel Repair

Profile