Part of the EllisLab Network
   
 
[Solved] - Problem in loading Validation Library
Posted: 16 October 2008 11:44 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  14
Joined  02-02-2007

Hi all,

I have presented my code below which causes error on my server. But i have tested the same code in some other server, it works fine.

If i load the validation library in the constructor then it’s working fine. But i needs to know why it has to be loaded in the constructor as i does not need the validation library in all the functions of the sample class.

I have also gone through the core files and found that there is function called “get_instance” with return the instance of the current controller (i.e., $this).

Instead of using like the following, which causes error

//Type 1
$this->validation->set_rules($rules); 

Alternatively i have tried using like the following which works fine.

//Type 2
$this->get_instance()->validation->set_rules($rules); 

To my knowledge $this && $this->get_instance() are the same.

If not, let me know the difference.

So may i know what causes error in the server. The same problem occurs while loading some models. Do i needs to check any php.ini settings ?

<?php
class Sample extends Controller
{
    
//Constructor
    
function Sample()
    
{
        parent
::Controller();
    
}

    
function index()
    
{
        
//Load the required libraries.
        
$this->load->library('validation');
        
        
//Set the validation rules
        
$this->_loginFrmRules();
    
}

    
function _loginFrmRules()
    
{
        
        $rules[
'admin_name']        'trim|required';
        
        
//Type 1 - Causes Error
        
$this->validation->set_rules($rules);
        
        
//Type 2 - Working Fine
        
$this->get_instance()->validation->set_rules($rules);

        
$fields['admin_name']        'Username';

        
$this->validation->set_fields($fields);
    
}
}
?> 
Profile
 
 
Posted: 17 October 2008 06:30 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  15
Joined  09-16-2008

Hi. I seem to have the opposite problem.

1) If I load the validation library in the constructor, the validation fields do not display.

2) If I load the validation library in the function, the validation fields display correctly.

The validation rule runs fine in either case (it correctly returns TRUE or FALSE).

This only happens on my Linux server. On my PC server its fine.

Profile
 
 
Posted: 17 October 2008 08:01 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  249
Joined  03-31-2007

The validation should work as you have the code, however, try:

<?php
class Sample extends Controller
{
    
//Constructor
    
function Sample()
    
{
        parent
::Controller();
    
}

    
function index()
    
{        
        
//Set the validation rules
        
$this->_loginFrmRules();
    
}

    
function _loginFrmRules()
    
{
        
//Load the required libraries.
        
$this->load->library('validation');

        
$rules['admin_name']        'trim|required';
        
        
//Type 1 - Causes Error
        
$this->validation->set_rules($rules);
        
        
//Type 2 - Working Fine
        
$this->get_instance()->validation->set_rules($rules);

        
$fields['admin_name']        'Username';

        
$this->validation->set_fields($fields);
    
}
}
?> 
Profile
 
 
Posted: 06 November 2008 02:05 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  14
Joined  02-02-2007

For me this solution got worked

Use the following line in the .htaccess file.

php_flag zend.ze1_compatibility_mode off

For more info have a look @ this

http://codeigniter.com/forums/viewthread/52308/P15/#450647

Profile
 
 
Posted: 18 August 2009 12:05 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  4
Joined  08-18-2009

Hi all,

A lot of time elapsed but I have similar problem while using set_rules() function from form_validation library. I tried to add

php_flag zend.ze1_compatibility_mode off 

line in .htaccess file but unfortunately I can’t do that on server where project is hosted. 

I added following line:

@ini_set('zend.ze1_compatibility_mode''0'); 

in index.php but it still doesn’t work.

Any ideas?

Profile