Part of the EllisLab Network
   
 
Pulling my hair out implementing form validation
Posted: 25 June 2009 11:45 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  68
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 02:31 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  134
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 04:38 PM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2915
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 07:29 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  68
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 07:40 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

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

Profile
 
 
Posted: 26 June 2009 08:23 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  7
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.

 Signature 

The early bird might get the worm, but the second mouse gets the cheese.
Unique Website Design

Profile
 
 
Posted: 26 June 2009 08:46 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

@VernonK: tried your suggestion, same result.

Profile
 
 
Posted: 26 June 2009 08:47 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2264
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 

Become a fan of the CodeIgniter Cookbook (estimated: Fall 2010).

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

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

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

 Signature 

The early bird might get the worm, but the second mouse gets the cheese.
Unique Website Design

Profile
 
 
Posted: 26 June 2009 09:26 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  68
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 03:13 PM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2915
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 09:39 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  68
Joined  04-02-2006

*bump* Anyone..?

Profile
 
 
Posted: 30 June 2009 10:40 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  39
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', $rules, TRUE) AND $result !== FALSE)
{
    
return;
}

(it’s around line 670)

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

How did you get on?

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115017 Total Logged-in Users: 56
Total Topics: 122449 Total Anonymous Users: 2
Total Replies: 647332 Total Guests: 564
Total Posts: 769781    
Members ( View Memberlist )