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">×</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">×</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(); ?>