• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Showing form validation errors individually using AJAX

#1
I want to individually show form validation errors under each input fields. I got it to work but the problem is if the field is already valid the element which handles the form error is not hiding.

Here's my ajax code:

Code:
function patient_add(){
    $.ajax({
        url: site_url('patient/patient_add'),
        data: $('#patient_form').serialize(),
        type: 'post',
        dataType: 'json',
       success: function(response){
        if(response.status === true){
            user_success(' fa fa-check-o','green',response.message,false);
            setTimeout(function(){window.location.href = response.page;},3000);
        }else{
                $.each(response.errors,function(key,val){
                    $('input[name="' + key + '"]').next().html(val).addClass('has-error');
                    $('select[name="' + key + '"]').next().html(val).addClass('has-error');
                });
            }
        }
    });
}
Here's my view:

Code:
<div class="col-md-6">
                             <div class="form-group">
                            <label for="firstname">First Name</label>
                            <input type="text" class="form-control" name="firstname">
                            <span class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="firstname">Middle Name</label>
                            <input type="text" class="form-control" name="middlename">
                            <span class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="firstname">Last Name</label>
                            <input type="text" class="form-control" name="lastname">
                            <span class="text-danger"></span>
                        </div>

Here's the controller portion.
Code:
 $error = array();
            foreach($this->input->post() as $key => $val){
              $error[$key] = form_error($key);
            }
            $response['errors'] = array_filter($error);
            $response['status'] = false;
Reply

#2
Add the removeClass before you call the aja to validate

function patient_add(){
// remove any previos error
$('input[name="' + key + '"]').next().html(val).removeClass('has-error');

then do the ajax to validcate
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#3
(07-26-2017, 07:35 AM)rtenny Wrote: Add the removeClass before you call the aja to validate

function patient_add(){
   // remove any previos error
   $('input[name="' + key + '"]').next().html(val).removeClass('has-error');

then do the ajax to validcate

i think it can't be done since the key and val are taken inside the ajax function
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.