Part of the EllisLab Network
   
 
Form validation not working with rules in config file
Posted: 03 July 2009 06:35 AM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  06-22-2009

I’m nearing completion of my first CI application. While going through my code doing a bit of housekeeping, I decided to move the form validation rules into a separate config file but can’t get it to work. I was defining the rules in the controller before which worked fine.

As far as I know I’ve followed the documentation to the letter and not tried to do any funny stuff so can’t figure out why its not working.

Are there any common pitfalls with doing this, not mentioned in the user guide, that I should be aware of?

Matt

Profile
 
 
Posted: 03 July 2009 07:00 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  9
Joined  06-22-2009

Perhaps I should add a little more information. As I said, before I moved the validation rules into the config file everything worked just fine.

I’ve got a controller (buy.php) with an index() method that looks like this (I’ve stripped out some additional code that isn’t relevant to the question to make things clearer):

<?
function index()
{
    
if ($_POST) {
        
if ($this->form_validation->run() == TRUE) {
            redirect
('buy/search');
        
}
        
else {
            $this
->load->view('buy');
        
}
    }
    
else {
        $this
->load->view('buy');
    
}
}
?>

I’m loading the form validation library in config/autoload.php

My config file (config/form_validation.php) looks like this:

<?
$config
= array(
    
'buy' => array(
        array(
            
'field' => 'make',
            
'label' => 'Make',
            
'rules' => 'required|not_default'
        
),
        array(
            
'field' => 'model',
            
'label' => 'Model',
            
'rules' => 'required|not_default'
        
),
        array(
            
'field'    => 'type',
            
'label'    => 'Type',
            
'rules'    => 'required|not_default'
    
),
        array(
            
'field'    => 'postcode',
            
'label'    => 'Postcode',
            
'rules'    => 'required|valid_postcode'
        
)
    )
);
?>

In reality I have more than one named group of rules in the $config array to apply to each method in the controller.

The custom functions are defined in MY_form_validation.php which extends the core form validation class.

I’m not getting any errors, and I know the config file is being loaded because I can echo stuff out from it. When I submit the form the page just reloads - obviously $this->form_validation->run() is returning FALSE.

Hope that helps put things in context a bit more.

Profile
 
 
Posted: 07 July 2009 04:32 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  06-22-2009

Hmmm, no-one got any ideas then?

Profile
 
 
Posted: 07 July 2009 04:42 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
RankRankRankRank
Total Posts:  1242
Joined  04-19-2008

Did you add the error strings to a language file?

 Signature 

PinoyTech - Web Development Blog

Profile
 
 
Posted: 07 July 2009 04:46 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  9
Joined  06-22-2009

No, I’m not using any language files.

Profile
 
 
Posted: 07 July 2009 04:50 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
RankRankRankRank
Total Posts:  1242
Joined  04-19-2008

Try using a language file for the validation like so:

class MY_Form_validation extends CI_Form_validation {

    
// functions from Kohana
    
function standard_text($str)
    
{

        
return (bool) preg_match('/^[\pL\pN\pZ\p{Pc}\p{Pd}\p{Po}]++$/uD', (string) $str);
    
}

    
function date_time($str)
    
{
        $valid
= strtotime($str);
        if (
$valid === FALSE OR $valid === -1)
        
{
            
return FALSE;
        
}
        
return TRUE;
    
}
}
$lang ['standard_text'] = 'The %s field must contain only letters, numbers, whitespace,dashes, periods, and underscores';
$lang ['date_time'] = 'The %s field must contain a valid date or time';

At least this is how I do it since I tend to use this later on in different projects.

 Signature 

PinoyTech - Web Development Blog

Profile
 
 
Posted: 09 July 2009 04:50 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  14
Joined  10-29-2007

You need to pass the group name to the run() function:

if ($this->form_validation->run('buy') == TRUE) {

The other alternative is to name your groups ‘class/function’ and then when run() is called from a particular function within a particular class, it finds the group automatically.

Caution: the ‘class/function’ method works only if there is no uri segment after class/function. The user’s guide doesn’t point out that limitation.

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 819, on March 11, 2010 11:15 AM
Total Registered Members: 120442 Total Logged-in Users: 44
Total Topics: 126530 Total Anonymous Users: 6
Total Replies: 665324 Total Guests: 354
Total Posts: 791854    
Members ( View Memberlist )