regex - Codeigniter 3 callback function not printing error in the view form -
first, possible add callback function within controller using (i have searched net , suggest not advisable do). in case, had declared callback function within controller using form validation. problem encounter is not prompting error regards syntax of username. think problem here? regex used in callback function? or problem not seeing thru, please me thanks! :(
the controller:
public function checklogin(){ //gets posted values $tempusername = $this->input->post('username'); $temppassword = $this->input->post('password'); if($this->validateinput()==false){ //if form data isn't accepted, loads login $this->load->view('bigphloginv'); }else{ //if form data accepted, checks 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 form data doesn't exist in db, loads login $this->load->view('bigphloginv'); }else{ //if form data exist on db, continues respective pages if($query=="admin"){ //if user admin redirect('bigphadmin/home'); }else if($query=="employee"){ redirect('bigphemployee/home'); //if user employee }//query type }//query false } }//validation false } //use validate input form public function validateinput(){ //set rules 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 form data isn't accepted, loads 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(); ?>
after check 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 line }else{ $this->load->view('formsuccess');//change line }
read more codeigniter form validation
Comments
Post a Comment