Welcome Guest, Not a member yet? Register   Sign In
validation php error with falidation fails
#1

[eluser]allibubba[/eluser]
hello there, been working woth codeigniter for a little while, trying to convert a previous site over to run the CI framework, so far i'm loving it, it's simple and intuitive, and so far have been able to overcome most of my problems, but this one has me a but confused.


here goes, i have a form with validation in my controller. my validation works great, returns correct error messages relevant to post parameters. the problem though, when validation does fail, i get my error messages, but i also get these two injected into my page:

A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: libraries/Validation.php
Line Number: 195

and

A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result to string conversion
Filename: libraries/Validation.php
Line Number: 195

i took a look at Validation.php:
line 195 : $ex = explode('|', $rules);

but when looking back through my controller i don't see any pipe problems?

here's my validation
Code:
$data['name']                     = "required|min_length[5]|max_length[125]";
$data['emailaddress']             = "required|min_length[5]|max_length[125]|valid_email";
$data['emailaddressconfirm']    = "callback_email_check";
$data['login']                     = "required|min_length[5]|max_length[125]|callback_login_check";
$data['password']                 = "required|min_length[5]|max_length[125]";
$data['passwordconfirn']         = "callback_password_check";
$data['level']                     = "callback_level_check";

any help or ideas would be great
#2

[eluser]Sarfaraz Momin[/eluser]
I see some callbacks there. This would not help can you paste the callback functions also and also what is line 195.
#3

[eluser]dbashyal[/eluser]
probably variable name error
$data inplace of $rules
validation looking for $rules

did u call set rules like this
Code:
$this->validation->set_rules($rules);
or,
Code:
$this->validation->set_rules($data);

i guess you have to pass $data i.e. 2nd one
#4

[eluser]allibubba[/eluser]
thanks for taking a look at this, i still haven't figured this out yet though...

here's line 195 from Validation.php
Quote:line 195 : $ex = explode(’|’, $rules);
which i then looked into my rules to see if i had missed any pipes and joined two rules, but they are all there

and rules are set with $data
Code:
$this->validation->set_rules($data);

here's my validation check
Code:
//set validation error output
        $this->validation->set_error_delimiters('<p class="error">', '</p>');
        if ($this->validation->run() == FALSE){
            // load layout        
            $this->layout->defaultLayout('contact_page',$data);
        }else{
            $this->db->insert('users', $_POST);
            
            //redirect after submitting
            redirect('contact/register_success');
        }

as for the callbacks, they all appear to be working, but here they are jsut in case i'm missing anything:

Code:
function email_check($str){
        //check if email addresses match
        if($_POST['emailaddress'] != $_POST['emailaddressconfirm']) {
            $this->validation->set_message('email_check', 'Email does not match.');
            return FALSE;
        }else {
            return TRUE;
        }
    }    
    function password_check($str){
        //check if passwords match
        if($_POST['password'] != $_POST['passwordconfirm']) {
            $this->validation->set_message('password_check', 'Passwords do not match.');
            return FALSE;
        }else {
            return TRUE;
        }
    }    
    function level_check($str){
        //check if default level is set to private
        if($_POST['level'] != 'private') {
            $this->validation->set_message('level_check', 'STOP... HAMMER TIME, you can only enlist as a private.');
            return FALSE;
        }else {
            return TRUE;
        }
    }    
    function login_check($str){
        //check if login exists, limit only to unique login names
        $this->db->where('login', $str);
                $data['query'] = $this->db->get('users');
        if(count($data['query']) > 0) {
            $this->validation->set_message('login_check', $str.' is already registered, please select something else');
            return FALSE;
        }else {
            return TRUE;
        }
    }

also, quick sub question. does this look like the best way to be validation these form fields, with several callbacks, or should i combine my callbacks into one function, and only have a callback on my last input?

thanks for the help so far.
#5

[eluser]Nikhil Vijayan[/eluser]
Change these lines in login_check callback

Code:
$data['query'] = $this->db->get('users');
    
if(count($data['query']) > 0) {


}

To this

Code:
$query = $this->db->get('users');

if($query->num_rows() > 0) {
            
}
#6

[eluser]dbashyal[/eluser]
am in a hurry but saw one spell mistake in rules, 'passwordconfirm' (m|n)
that may not be the cause.
will see it 2moro after work.
#7

[eluser]allibubba[/eluser]
Nikhil, thanks for the suggestion, updated my code, but no luck.

dbashyal, yeah, i caught that after i posted. i've since commented out all validation except for my first field;
Code:
$data['name'] = "required|min_length[5]|max_length[125]";

now, i can submit fine if i meet those requirements, (5 to 125 characters). but if i fail my validation i still get that php error. starting to wonder if it is normal and if i had error reporting off i would not have the problem, but that doesn't seem right either, if validation fails i should just return to my registration form with error messages displayed, right?

here's my failure statement;
Code:
//set validation error output
$this->validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->validation->run() == FALSE){
// load layout        
  $this->layout->defaultLayout('contact_page.php',$data);
}else{
  $this->db->insert('users', $_POST);
  //redirect after submitting
  redirect('contact/register_success');
}
#8

[eluser]Nikhil Vijayan[/eluser]
you have pointed the error is at this line

Code:
$ex = explode('|', $rules);

so in validation library above this code just add this

Code:
var_dump($rules);

and let me know whats the output when you are getting the error


Also what i understand is you are passing a mysql result into it somehow.. ..
#9

[eluser]allibubba[/eluser]
when i get home from work i'll put the whole page up, see if there is anything i'm missing, and i'll run var_dump(), see what i get from that as well.
#10

[eluser]allibubba[/eluser]
here's what i get from the dump;
Code:
string(4) "form" string(4) "FORM" string(7) "contact" object(CI_DB_mysql_result)#15 (7) { ["conn_id"]=>  resource(28) of type (mysql link persistent) ["result_id"]=>  resource(36) of type (mysql result) ["result_array"]=>  array(0) { } ["result_object"]=>  array(0) { } ["current_row"]=>  int(0) ["num_rows"]=>  int(1) ["row_data"]=>  NULL }

i've posted my entire contact controller (should actually be registration, but change that later)

contact controller

view source of page to see formatted php.

again, thanks for any help or ideas




Theme © iAndrew 2016 - Forum software by © MyBB