Welcome Guest, Not a member yet? Register   Sign In
Validation Callback function not working
#1

[eluser]terry101[/eluser]
I currently worked on building verification using the callback function. For some reason the callback function isn’t working. Other verification functions work.
“dbcheck” is the function which isn’t working on the script below. I dont get any errors either.

Controller
Code:
function register(){
        
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|xss_clean|callback_dbcheck');
            $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('email', 'E-mail', 'required|min_length[3]|max_length[20]|xss_clean|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('password_conf', 'Re-type Password', 'required|min_length[3]|max_length[20]|xss_clean|matches[password]');
    
            if ($this->form_validation->run() == FALSE) {
            
                $this->load->view('viewregister');
            }
                else {
                $username = $this->input->post('username');
                $name = $this->input->post ('name');
                $email = $this->input->post ('email');
                $password = $this->input->post ('password');
                $this->Usermodel->register_user($username,$name, $email, $password);
                
                echo " you have been registered $username";
                
            }
        }
        
            function dbcheck ($username) {

            $checkusername = array('username' => $username);
            $verifyusername = $this->db->get_where('tbregister', $checkusername);
            if ($verifyusername->num_rows == 1) {
                
            $this->form_validation->set_message('dbcheck','%s already exists!');
                return FALSE;
            }
            else {
                
                return TRUE;
            }
        }
#2

[eluser]Mirge[/eluser]
What do you mean by "doesn't work"? No error messages... is the function being called (dbcheck)? Have you verified it's being called? Is this code in a controller or model?

More information please.
#3

[eluser]terry101[/eluser]
the function is in controller. yes its the function called dbcheck. i dont get any error, but i tested it and when inserting the same username from the one which is already in the database it doesnt give me a error and it would insert that username.

Controller (the function on the controller called dbcheck, i added dbcheck in the validation for username)

Code:
function register(){
        
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|xss_clean|callback_dbcheck');
            $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('email', 'E-mail', 'required|min_length[3]|max_length[20]|xss_clean|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('password_conf', 'Re-type Password', 'required|min_length[3]|max_length[20]|xss_clean|matches[password]');
    
            if ($this->form_validation->run() == FALSE) {
            
                $this->load->view('viewregister');
            }
                else {
                $username = $this->input->post('username');
                $name = $this->input->post ('name');
                $email = $this->input->post ('email');
                $password = $this->input->post ('password');
                $this->Usermodel->register_user($username,$name, $email, $password);
                
                echo " you have been registered $username";
                
            }
        }
        
            function dbcheck ($username) {

            $checkusername = array('username' => $username);
            $verifyusername = $this->db->get_where('tbregister', $checkusername);
            if ($verifyusername->num_rows == 1) {
                
            $this->form_validation->set_message('dbcheck','%s already exists!');
                return FALSE;
            }
            else {
                
                return TRUE;
            }
        }
#4

[eluser]Mirge[/eluser]
num_rows is a method, not a property. Change $verifyusername->num_rows to $verifyusername->num_rows()

It should read:

Code:
if($verifyusername->num_rows()) { ... username already exists code here ... }
#5

[eluser]terry101[/eluser]
i tried that, still have the same problem

Controller

Code:
function dbcheck ($username) {
    
            $checkusername = array('username' => $username);
            $verifyusername = $this->db->get_where('tbregister', $checkusername);
            if ($verifyusername->num_rows() == 1) {
                
            $this->form_validation->set_message('dbcheck','%s already exists!');
                return FALSE;
            }
            else {
                
                return TRUE;
            }
#6

[eluser]jblack199[/eluser]
try your sql in a different way...

Code:
$this->db->where('username', $username);
$verifyusername = $this->db->get('tbregister');

and see what comes of that...
#7

[eluser]Mirge[/eluser]
echo $verifyusername->num_rows().. make sure the value IS actually "1". I purposely left off the "==1" part.
#8

[eluser]terry101[/eluser]
jblack199 i tried what you suggested but no dice. Mirge the 1 was always included and no dice either. Any other suggestions on what could be possibly wrong?


Code:
function dbcheck ($username) {
    
            $checkusername = array('username' => $username);
            $verifyusername = $this->db->get_where('tbregister', $checkusername);
var_dump($verifyusername);
//$this->db->where('username', $username);
//$verifyusername = $this->db->get('tbregister');
            if ($verifyusername->num_rows() == 1) {
                
            $this->form_validation->set_message('dbcheck','%s already exists!');
                return FALSE;
            }
            else {
                
                return TRUE;
            }
        }

i tried var_dump($verifyusername); and i get nothin below, so nothing getting passes?

object(CI_DB_mysql_result)[17]
public 'conn_id' => resource(29, mysql link persistent)
public 'result_id' => resource(34, mysql result)
public 'result_array' =>
array
empty
public 'result_object' =>
array
empty
public 'custom_result_object' =>
array
empty
public 'current_row' => int 0
public 'num_rows' => int 16
public 'row_data' => null
#9

[eluser]jblack199[/eluser]
i've never been a fan of var_dump try:

Code:
echo "<pre>";
print_r($verifyusername);
echo "</pre>";

I think it puts it into an easier to read fashion... and you've echo'd out $verifyusername->num_rows() and checked to see what it returns

but try changing $verifyusername->num_rows to $verifyusername->count_all('tbregister') and see if that fixes it...
#10

[eluser]terry101[/eluser]
I have tried
Code:
echo "<pre>";
print_r($verifyusername);
echo "</pre>";
but i get a error on that line of code. I have also tried
Code:
$verifyusername->count_all(‘tbregister’)
as you suggested but i only get a error on that line. Any other ideas? could it be a setting issue in config?




Theme © iAndrew 2016 - Forum software by © MyBB