0

First, is it possible to add callback function within the controller that is using this (I have searched the net and they suggest that it is not advisable to do). So in my case, i had declared the callback function within the controller that is using the form validation. The problem that i encounter is that it is not prompting an error with regards to the syntax of the username. So what do you think is the problem here? is it the regex that i used in the callback function? or is it a problem that i am not seeing thru, please help me thanks! :(

The Controller:

public function checkLogIn(){
    //Gets the posted values
    $tempUsername = $this->input->post('username');
    $tempPassword = $this->input->post('password');

    if($this->validateInput()==false){ //If the form data isn't accepted, loads back to login
        $this->load->view('bigphloginv');
    }else{ //If form data is accepted, checks the database
        if($this->session->userdata('username')==$tempUsername){
            $this->load->view('bigphloginv');
        }else{
            $this->load->model('bigphuser');
            $query = $this->bigphuser->login($tempUsername,$tempPassword);
            if($query==FALSE){ //If the form data doesn't exist in db, loads back to login
                $this->load->view('bigphloginv');
            }else{ //If the form data exist on db, then continues to their respective pages
                if($query=="admin"){ //if the user is an admin
                    redirect('bigphadmin/home');
                }else if($query=="employee"){
                    redirect('bigphemployee/home'); //if the user is an employee
                }//Query Type
            }//Query false
        }
    }//validation false
}

//Use to validate the input form
public function validateInput(){

    //Set the rules for forms
    $this->form_validation->set_rules('username', 'Username','trim|required|min_length[4]|max_length[50]|callback_check_username');
    $this->form_validation->set_rules('password', 'Password','trim|required|min_length[8]|max_length[50]');

    if($this->form_validation->run()==false){ //If the form data isn't accepted, loads back to login
        return false;
    }else{
        return true;
    }
}

public function check_username(){
    $tempUsername = $this->input->post('username');
    $regex = "/^[a-zA-Z0-9]*([\._]?[a-zA-Z0-9])*/";
    if(preg_match($regex, $tempUsername)){
        return true;
    }else{
        $this->form_validation->set_message('check_username','Invalid Username syntax');
        return false;
    }
}

The View

                    <?php echo form_open('bigphloginc/checkLogin');?>
                        <div class="form-group">
                            <?php if(form_error('username'))echo  
                            '<div class="alert alert-warning alert-dismissible" id="errorDiv" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'
                                .form_error('username').
                            '</div>';
                            ?>
                            <input type="text" class="form-control" id="usernameId" name="username" value="<?php echo set_value('username');?>" placeholder="Username">
                        </div>
                        <div class="form-group">
                            <?php if(form_error('password'))echo  
                            '<div class="alert alert-warning alert-dismissible" id="errorDiv" role="alert" >
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'
                                .form_error('password').
                            '</div>';
                            ?>  
                            <input type="password" class="form-control" id="passwordId" name="password" value="<?php echo set_value('password');?>" placeholder="Password">
                        </div>
                        <button type="submit" class="btn btn-default" style><span class="glyphicon glyphicon-log-in"></span> Log in</button>
                    <?php echo form_close(); ?>

2 Answers 2

0

After you check your validation load related views.

Example

public function checkUsername(){
    $this->form_validation->set_message('checkUsername','Invalid Username syntax');
    $tempUsername = $this->input->post('username');
    $regex = "/^[a-zA-Z0-9]*([\._]?[a-zA-Z0-9])*/";
    if(preg_match($regex, $tempUsername)){
        $this->load->view('myform');//change this line
    }else{
        $this->load->view('formsuccess');//change this line too
    }

Read more about Codeigniter Form Validation

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, yes. You can add callback function within the controller that is using this.

Secondly, the callback method name and all subsequent references to it as a validation rule, should be all lowercase.

Please check:

http://www.codeigniter.com/user_guide/libraries/form_validation.html?highlight=form%20validation#callbacks-your-own-validation-methods

and

Codeigniter 2 - Callback function in My_Controller

3 Comments

tried lowering the case but still doesn't load the error :/
Try to create an example given in Codeigniter's website (Follow the first link). That is remove the regex and try a simple check. If it's work than problem is in your regex.
Well all the other rules are working in the validation->set_rule() and they are even prompting the error properly, but the problem is that i've already checked my regex and it's working properly o.O also you might want to recheck the code i've posted because i've updated the missing structure my bad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.