0

I'm trying to validate the form fields on submit. I am successfully getting the below JSON error validation messages in the array. But how can I loop through this so that each error is shown below respective field?

Error Return json:

{
  "data": {
    "error": {
      "first_name": "First name is required",
      "last_name": "Last name is required",
      "email":"Email is required",
      "phone":"Phone is required"
    }
  }
}
$.ajax({
  url: '<?php echo $this->CxHelper->Route('
  eb - front - validate - evaluation - quiz - client - details '); ?>',
  type: 'POST',
  data: $('#signup-form').serialize(),
  dataType: 'JSON',
  success: function(data) {
    if (data.status == 'success') {
      console.log(data.status);
    }
  },
  error: function(data) {
    console.log(data.responseText);

    // i want to loop here so that each error is shown
    // below respective field
    $('.error-box').html(data.responseText);
  }
});
<div class='content'>
  <div class="top-title">Body Evaluation</div>
  <div class="row">
    <div class="col-md-6 col-sm-12 col-xs-12 evaluation-quiz-user-detail-div left-side-border-right">
      <div class="error-box"></div>
      <div class="row">
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("first_name")}}</label>
          <label class="input">
           {{ EvaluationClientFrontEndForm.render("first_name", ["class": "form-control"]) }}
          </label>
        </section>
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("last_name")}}</label>
          <label class="input">
           {{ EvaluationClientFrontEndForm.render("last_name", ["class": "form-control"]) }}
         </label>
        </section>
      </div>
      <div class="row">
        <section class="col-md-12">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("email")}}</label>
          <label class="input make-me-block">
            {{ EvaluationClientFrontEndForm.render("email", ["class": "form-control"]) }}
           </label>
        </section>
      </div>
      <div class="row">
        <section class="col-md-12">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("phone")}}</label>
          <label class="input make-me-block">
            {{ EvaluationClientFrontEndForm.render("phone", ["class": "form-control"]) }}
          </label>
        </section>
      </div>

      <div class="row">
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("age")}}</label>
          <label class="input">
            {{ EvaluationClientFrontEndForm.render("age", ["class": "form-control"]) }}
          </label>
        </section>
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("gender")}}</label><br>
          <input type="radio" name="gender" value="male"> Male
          <input type="radio" name="gender" value="female"> Female<br>
        </section>
      </div>
      <div class="row">
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("current_weight")}}</label>
          <label class="input">
            {{ EvaluationClientFrontEndForm.render("current_weight", ["class": "form-control"]) }}
          </label>
        </section>
        <section class="col-md-6">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("goal_weight")}}</label>
          <label class="input">
            {{ EvaluationClientFrontEndForm.render("goal_weight", ["class": "form-control"]) }}
          </label>
        </section>
      </div>
      <div class="row">
        <section class="col-md-12">
          <label class="label">{{EvaluationClientFrontEndForm.getLabel("skype_id")}}</label>
          <label class="input make-me-block">
            {{ EvaluationClientFrontEndForm.render("skype_id", ["class": "form-control"]) }}
          </label>
        </section>
      </div>
    </div>

For now, I'm showing the error like this in a div box:

enter image description here

Please let me know if any further detail is required I will share it.

Thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Imad uddin
  • 147
  • 1
  • 2
  • 11
  • `data.error` is an object, not an array. As such you'll need to loop through it differently. See the duplicate I marked for more information and an example of how to do it. – Rory McCrossan Oct 23 '18 at 16:19
  • @RoryMcCrossan i tried but isn't working with me! Can you please give example with my code. – Imad uddin Oct 23 '18 at 16:48

0 Answers0