php - There are some unexpected errors in form validation in codeigniter 3 -
i making 2 step registration. have done first step, doing second one. want make ajax form validation, gives error right away opening page , error @ top of page
pressing submit button gives no errors despite empty fields.
here view:
<div id="messages"></div> <?php $attributes = array('class' => 'rex-forms', 'name' => 'continueregistrationform', 'id' => 'continueregistrationform'); ?> <?= form_open_multipart('user/continueregistration', $attributes); ?> <div class="container-fluid"> <div class="row"> <div class="col-md-7"> <div class="row"> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-user" aria-hidden="true"></i></span> <input id="name" type="text" class="form-control" name="name" placeholder="name" value="<?= $instructors['name']; ?>"> </div><br> </div> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-globe" aria-hidden="true"></i></span> <input id="web" type="text" class="form-control" name="web" placeholder="web-site" value="<?= $instructors['web']; ?>"> </div><br> </div> </div> <div class="row"> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-phone" aria-hidden="true"></i></span> <input id="tel" type="text" class="form-control" name="tel" placeholder="phone" value="<?= $instructors['phone']; ?>"> </div><br> </div> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-map-marker" aria-hidden="true"></i></span> <input id="address" type="text" class="form-control" name="address" placeholder="address" value="<?= $instructors['address']; ?>"> </div><br> </div> </div> <div class="row"> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-facebook-square" aria-hidden="true"></i></span> <input id="facebook" type="text" class="form-control" name="facebook" placeholder="facebook" value="<?= $instructors['fb']; ?>"> </div><br> </div> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-twitter-square" aria-hidden="true"></i></span> <input id="twitter" type="text" class="form-control" name="twitter" placeholder="twitter" value="<?= $instructors['twitter']; ?>"> </div><br> </div> </div> <div class="row"> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-youtube-square" aria-hidden="true"></i></span> <input id="youtube" type="text" class="form-control" name="youtube" placeholder="youtube" value="<?= $instructors['youtube']; ?>"> </div><br> </div> <div class="col-md-6 col-sm-6"> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-instagram" aria-hidden="true"></i></span> <input id="instagram" type="text" class="form-control" name="instagram" placeholder="instagram" value="<?= $instructors['instagram']; ?>"> </div><br> </div> </div> <div class="row"> <div class="col-md-12 col-sm-12"> <div class="col-md-12 input-group" id="textareadescp"> <textarea name="insdescription" class="form-control" rows="5" id="profiledesc" placeholder="description"><?= $instructors['description']; ?></textarea> </div><br><br> </div> <!-- <script> ckeditor.replace('profiledesc'); </script> --> </div> <div class="row"> <div class="col-md-8 col-sm-12"> </div> <div class="col-md-4"> <div class="modal-footer btncolor"> <button type="submit" name="submit" id="submit" class="rex-bottom-medium rex-btn-icon"> <span class="rex-btn-text">submit</span> <span class="rex-btn-text-icon"><i class="fa fa-arrow-circle-o-right"></i></span> </button> </div> </div> </div> </div> </div> </div> </form>
here controller:
function continueregistration() { //set validation rules $validator = array('success' => false, 'messages' => array()); $validate_data = array( array( 'field' => 'name', 'label' => 'name', 'rules' => 'trim|required|min_length[2]|max_length[30]' ), array( 'field' => 'web', 'label' => 'web adress', 'rules' => 'trim|required|valid_url|prep_url|min_length[3]' ), array( 'field' => 'facebook', 'label' => 'facebook adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'twitter', 'label' => 'twitter adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'twitter', 'label' => 'twitter adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'youtube', 'label' => 'youtube adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'instagram', 'label' => 'instagram adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'tel', 'label' => 'telephone number', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]' ), array( 'field' => 'address', 'label' => 'adress', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]' ), array( 'field' => 'insdescription', 'label' => 'description', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]' ) ); $this->form_validation->set_rules($validate_data); $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>'); $data['title'] = 'continue registration'; $data['instructors'] = $this->user_model->getuserinfoforreg(); $this->load->view('templates/header'); $this->load->view('registration/registration', $data); $this->load->view('templates/footer'); //validate form input if ($this->form_validation->run() === false) { // fails $validator['success'] = false; foreach ($_post $key => $value) { $validator['messages'][$key] = form_error($key); } } else { //insert user registration details database $data = array( 'name' => $this->input->post('name'), 'web' => $this->input->post('web'), 'fb' => $this->input->post('facebook'), 'twitter' => $this->input->post('twitter'), 'youtube' => $this->input->post('youtube'), 'instagram' => $this->input->post('instagram'), 'phone' => $this->input->post('tel'), 'address' => $this->input->post('address'), 'description' => $this->input->post('insdescription') ); $id = $this->session->userdata('id'); // insert form data database if ($this->user_model->updateuser($id, $data)) { $validator['success'] = true; $validator['messages'] = array(); } else { // error $validator['success'] = false; $validator['messages'] = '<div class="alert alert-danger text-center">error</div>'; } } echo json_encode($validator); }
here ajax form:
$(document).ready(function() { $("#continueregistrationform").unbind('submit').bind('submit', function() { var form = $(this); $.ajax({ url: form.attr('action'), type: form.attr('method'), data: form.serialize(), datatype: 'json', success:function(response) { console.log(response); if(response.success) { $("#messages").html(response.messages); $("#continueregistrationform")[0].reset(); $(".text-danger").remove(); $(".form-group").removeclass('has-error').removeclass('has-success'); } else { $.each(response.messages, function(index, value) { $("#messages").html(response.messages); var element = $("#"+index); $(element) .closest('.form-group') .removeclass('has-error') .removeclass('has-success') .addclass(value.length > 0 ? 'has-error' : 'has-success') .find('.text-danger').remove(); $(element).after(value); }); } } // /success }); // /ajax return false; }); });
try load view on different function have seperate function submit form. show model also
<?php class user extends ci_controller { class function continueregistration() { $data['title'] = 'continue registration'; $data['instructors'] = $this->user_model->getuserinfoforreg(); $this->load->view('templates/header'); $this->load->view('registration/registration', $data); $this->load->view('templates/footer'); } public function submit() { $json = array(); $validate_data = array( array( 'field' => 'name', 'label' => 'name', 'rules' => 'trim|required|min_length[2]|max_length[30]' ), array( 'field' => 'web', 'label' => 'web adress', 'rules' => 'trim|required|valid_url|prep_url|min_length[3]' ), array( 'field' => 'facebook', 'label' => 'facebook adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'twitter', 'label' => 'twitter adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'twitter', 'label' => 'twitter adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'youtube', 'label' => 'youtube adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'instagram', 'label' => 'instagram adress', 'rules' => 'trim|valid_url|prep_url|min_length[3]' ), array( 'field' => 'tel', 'label' => 'telephone number', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]' ), array( 'field' => 'address', 'label' => 'adress', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]' ), array( 'field' => 'insdescription', 'label' => 'description', 'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]' ) ); $this->form_validation->set_rules($validate_data); $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>'); if ($this->form_validation->run() == false) { foreach ($_post $key => $value) { $json['messages'][$key] = form_error($key); } } else { //insert user registration details database $data = array( 'name' => $this->input->post('name'), 'web' => $this->input->post('web'), 'fb' => $this->input->post('facebook'), 'twitter' => $this->input->post('twitter'), 'youtube' => $this->input->post('youtube'), 'instagram' => $this->input->post('instagram'), 'phone' => $this->input->post('tel'), 'address' => $this->input->post('address'), 'description' => $this->input->post('insdescription') ); // not set sessions gets it. $update_user = $this->user_model->updateuser($this->session->userdata('id'), $data) if ($update_user)) { $json['success'] = true; } else { $json['messages'] = '<div class="alert alert-danger text-center">error</div>'; } } echo json_encode($json); } }
ajax
$(document).ready(function() { $("#submit").on('click', function() { $.ajax({ url: "<?php echo base_url('user/submit');?>", type: "post", data: $("#continueregistrationform").serialize(), datatype: 'json', success:function(response) { console.log(response); if(response.success) { $("#messages").html(response.messages); $("#continueregistrationform")[0].reset(); $(".text-danger").remove(); $(".form-group").removeclass('has-error').removeclass('has-success'); } else { $.each(response.messages, function(index, value) { $("#messages").html(response.messages); var element = $("#"+index); $(element) .closest('.form-group') .removeclass('has-error') .removeclass('has-success') .addclass(value.length > 0 ? 'has-error' : 'has-success') .find('.text-danger').remove(); $(element).after(value); }); } } // /success }); // /ajax return false; }); });
view
<?php $attributes = array('class' => 'rex-forms', 'id' => 'continueregistrationform'); ?> <?php echo form_open_multipart('user/submit', $attributes); ?> <?php echo form_close();?>
Comments
Post a Comment