Part of the EllisLab Network
   
2 of 12
2
Who wants to test the new CI Form Validation class?
Posted: 26 August 2008 06:44 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  04-25-2006

Is anyone else having trouble getting the new form validation helper functions to work?

I copied the tutorial in the user guide exactly, but I got no error messages when submitting the form. I traced the problem to the _get_validation_object method in the form helper file. It looks like it isn’t returning the form_validation object. I made a few changes to the code and I was able to get it working, but I’m not sure if its the code or if I just did something wrong on my end.

 Signature 

My website: http://jamesgifford.com

Profile
 
 
Posted: 26 August 2008 06:45 PM   [ Ignore ]   [ # 17 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  19272
Joined  06-03-2002

What version of PHP are you running, James?  And at which point in _get_validation_object() is it failing?  Do a var_dump() please of the variables in the errant conditional and then paste the result here if you would.

 Signature 
Profile
MSG
 
 
Posted: 26 August 2008 06:50 PM   [ Ignore ]   [ # 18 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  04-25-2006

For me, the _get_validation_object() function is returning false at line 946 in the form_helper file. This is causing the validation_errors() function to return an empty string at line 819 (similar for form_error()). I fixed this by changing the _get_validation_object() function from this:

function &_get_validation_object()
    
{
        $CI
=& get_instance();
        
        
// We set this as a variable since we're returning by reference
        
$return = FALSE;
        
        if ( ! isset(
$CI->load->_ci_classes) OR  ! isset($CI->load->_ci_classes['form_validation']))
        
{
            
return $return;
        
}
        
        $object
= $CI->load->_ci_classes['form_validation'];
        
        if ( ! isset(
$CI->$object) OR ! is_object($CI->$object))
        
{
            
return $return;
        
}
        
        
return $CI->$object;
    
}

to this:

function &_get_validation_object()
    
{
        $CI
=& get_instance();
        
        
// We set this as a variable since we're returning by reference
        
$return = FALSE;
        
        if ( ! isset(
$CI->load->_ci_classes) OR  ! empty($CI->load->_ci_classes['form_validation']))
        
{
            
return $return;
        
}
        
        $object
= 'form_validation';
        
        if ( ! isset(
$CI->$object) OR ! is_object($CI->$object))
        
{
            
return $return;
        
}
        
        
return $CI->$object;
    
}

Basically the problem seems to stem from $CI->input->_ci_classes[‘form_validation’] being set to null.

Oh, and I’m running PHP 5.2.6

 Signature 

My website: http://jamesgifford.com

Profile
 
 
Posted: 26 August 2008 07:07 PM   [ Ignore ]   [ # 19 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I just glanced over the new documentation and code and i have to say this is a big step forward.

The thing i still miss the most is when setting a custom message you need to load an extra language file if you don’t want to hard code the message.

$this->form_validation->set_message('required', 'Your custom message here');
// vs
$this->form_validation->set_message('required', 'required_custom');

Keys have no spaces so that’s how you could separate hard coded strings from language file lines.

function set_message($lang, $val = '')
    
{
        
if ( ! is_array($lang))
        
{
            
if(str_pos($val,' ') === FALSE)
                        
{
                             $this
->CI->lang->load('form_validation');
                             if (
FALSE === ($line = $this->CI->lang->line($val)))
                 
{
                                 
return FALSE;
                             
}

                             $lang
= array($lang => $line);
                        
}
                        
else
                        
{
                             $lang
= array($lang => $val);
                        
}
        }
    
        $this
->_error_messages = array_merge($this->_error_messages, $lang);
    
}

It’s a quick example how it could be done, it looks ugly.

Profile
 
 
Posted: 26 August 2008 11:33 PM   [ Ignore ]   [ # 20 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2916
Joined  07-27-2006

Great catch, James! Your version of _get_validation_object() got it working for me too.

So besides that glitch, I love what I’ve seen. set_value(), love it. validation_errors(), form_error(), love those too. Autoloading validation rules based on controller/method, too-too good!

 Signature 

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

Profile
 
 
Posted: 26 August 2008 11:53 PM   [ Ignore ]   [ # 21 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2512
Joined  12-21-2001

Actually, James and Colin, I think the bug is in the Loader.php class.  If you wander over to line 912 and change this:

$this->_ci_classes[$class] = $object_name;

to this:

$this->_ci_classes[$class] = $classvar;

I believe it’ll fix it.  I’d appreciate it if you guys could check that for me.  Thanks…

 Signature 
Profile
MSG
 
 
Posted: 27 August 2008 12:05 AM   [ Ignore ]   [ # 22 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  04-25-2006

Yep, that fixed it.

I didn’t think to trace the error back to the loader class. This must have effected more than just the form_validation library.

 Signature 

My website: http://jamesgifford.com

Profile
 
 
Posted: 27 August 2008 12:06 AM   [ Ignore ]   [ # 23 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2916
Joined  07-27-2006

Ah.. makes sense, Rick. Fixed it for me too. Running PHP Version 5.2.5, just to clarify.

 Signature 

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

Profile
 
 
Posted: 27 August 2008 12:14 AM   [ Ignore ]   [ # 24 ]  
Lab Assistant
RankRank
Total Posts:  143
Joined  02-16-2008

EDIT: never mind, fixed in SVN

Other than that new validation library looks great.

Profile
 
 
Posted: 27 August 2008 12:16 AM   [ Ignore ]   [ # 25 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  04-25-2006

steelaz: check out Rick’s fix in the Loader class, it’s the correct way to fix the error.

 Signature 

My website: http://jamesgifford.com

Profile
 
 
Posted: 27 August 2008 12:20 AM   [ Ignore ]   [ # 26 ]  
Lab Assistant
RankRank
Total Posts:  143
Joined  02-16-2008

Yep, sorry, forgot to export new revision to my project. Looks like Loader.php is fixed now in 1318

Profile
 
 
Posted: 27 August 2008 12:42 AM   [ Ignore ]   [ # 27 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2916
Joined  07-27-2006

Yep. 1318 is solid for me too.

 Signature 

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

Profile
 
 
Posted: 27 August 2008 02:35 AM   [ Ignore ]   [ # 28 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I’m kicking the tires at the moment and i noticed the language file contains following lines

$lang['numeric']            = "The %s field must contain a number.";
$lang['is_numeric']            = "The %s field must contain a number.";
$lang['is_natural']            = "The %s field must contain a number.";

Why not do

$lang['is_natural']            =
$lang['is_numeric']            =
$lang['numeric']            = "The %s field must contain a number.";

Another thing i noticed. Now that the field labels are attached to the rules shouldn’t there be language dependend config files? It’s a wasps nest because it’s a tough problem to solve with a simple solution but if you want to support multiple languages at the moment there is no other way than to put the rules in the controllers because of it. And with all the config goodies provided by the new validation library it’s a bit of a shame.
Maybe a form_validation_labels file could solve the problem?

EDIT : if the form_validation_labels file is going to be used it has another benefit. You could add a form_label function to the form validation helper. i know it’s spreading the html tag generating functions over multiple files but then you can move it out of the language class code which is the bigger evil of the two IMO.

Profile
 
 
Posted: 27 August 2008 08:20 AM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  548
Joined  07-28-2008

The new set_value is great. However, what do you think is the preferred method for setting this value for say an edit record page?

I tend to prefer to set the default values within the controllers themselves, whether pulled from the database or not.

So does this mean simply calling set_values from within a controller will set a default value, then we can call set_values in the view without specifying a default value?

Should I switch my methodology and pass all the ‘edited’ start data to the view and simply load it from there?

Has anyone had a chance to mull this over and what they feel works the best for them?

Previously I used the set_default_values hack within the controller itself.

 Signature 

~ 4 All the Right Reasons ~

Profile
 
 
Posted: 27 August 2008 10:15 AM   [ Ignore ]   [ # 30 ]  
Lab Assistant
RankRank
Total Posts:  143
Joined  02-16-2008

When adding rule ‘integer’, even if field is not required, it will throw an error when field is empty.

Can someone confirm?

EDIT: This modification in Form_validation.php line 483 fixed it for me:

From:

if ( ! in_array('required', $rules, TRUE) AND is_null($postdata))

To:

if ( ! in_array('required', $rules, TRUE) AND empty($postdata))
Profile
 
 
   
2 of 12
2
 
‹‹ first_row, last_row      Nice Dates ››
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: 120010 Total Logged-in Users: 43
Total Topics: 126142 Total Anonymous Users: 2
Total Replies: 663554 Total Guests: 433
Total Posts: 789696    
Members ( View Memberlist )