1

I want to validate NID number is exists or not. I want this validation on key up via AJax. I didn't work to show errors in ajax before. I write these codes in Jquery. Please help me if I have any mistakes.

in blade

<div class="form-group row">
    <label for="nid" class="col-sm-2 col-form-label">
        NID Number<sup class="text-danger">*</sup>
    </label>
    <div class="col-sm-10">
        <input type="text"
               class="form-control {!! $errors->has('nid_number') ? 'is-invalid' : 'is-valid' !!}"
               placeholder="ভোটার আইডি" id="nid"
               name="nid_number" value="{{ old('nid_number') }}">
        @error('nid_number')
        <span class="invalid-feedback" role="alert">
            <strong>{{ $message }}</strong>
        </span>
        @enderror
    </div>
</div>

jquery cdn

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

ajax code

$(document).on('keyup', '#nid', function(){
    $.ajax({
        url:"{{ route('ajax-validation') }}",
        method:'POST',
        data:{query:$(this).val()},
        dataType:'json',
        success:function(data)
        {
            alert(data);
        }
    })
});

in controller

public function ajaxValidation(Request $request)
{
    if ($request->ajax()) {
        $this->validate($request, [
            'nid_number' => 'unique:members',
        ]);
    }
}

I think this validate() returns errors automatically. that's why I didn't use any return json_enconde() Now help me How can I show errors now. Thanks in advance. And sorry for your time.

STA
  • 30,729
  • 8
  • 45
  • 59
tariqul anik
  • 314
  • 6
  • 24
  • This is some link that could help you: https://stackoverflow.com/questions/10931836/should-i-use-done-and-fail-for-new-jquery-ajax-code-instead-of-success-and Modifiying the way you do your ajax request and using the ```.fail()``` will ease your way to display errors. – ettdro Jun 05 '20 at 17:24
  • Return output from `ajaxValidation` via json. And catch it with ajax and show the error – STA Jun 05 '20 at 17:39
  • how can I return errors from validate($request)? Can you please tell me? @TalhaF. – tariqul anik Jun 05 '20 at 17:42
  • Please provide your JSON response code from `ajaxValidation` – STA Jun 05 '20 at 17:43
  • I just want to check that NID is existed or not. ```if ($request->ajax()) { $this->validate($request, [ 'nid_number' => 'unique:members', ]); }``` that is the validation code. It automatically return errors when I normally submit the form. but I dont know how to send errors in json, that's why I asked. – tariqul anik Jun 05 '20 at 17:47
  • Check `request` from your browser and see the output. – STA Jun 05 '20 at 17:48
  • As further I know, laravel `validation` send data by SESSION in server side. – STA Jun 05 '20 at 17:49
  • yeah right..... – tariqul anik Jun 05 '20 at 17:54
  • I think, my flow of validation using ajax was wrong. Can you suggest me any documents to check live validation using ajax? – tariqul anik Jun 05 '20 at 17:56

2 Answers2

1

You can check validation like this :

public function ajaxValidation(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nid_number' => 'unique:members',
        ]);
        if ($validator->passes()) {
            return response()->json(['status' => '1']); // success
        }
        return response()->json(['status' => '0'); // not success
    }
STA
  • 30,729
  • 8
  • 45
  • 59
  • then How to handle from ajax? In ajax its always say error here is the updated ajax code, `$(document).on('keyup', '#nid', function () { var request = $.ajax({ url: "{{ route('ajax-validation') }}", method: 'POST', data: {query: $(this).val()}, }).fail(function () { alert('error'); }).done(function () { alert('done') }); });` – tariqul anik Jun 05 '20 at 18:14
  • it always go on .`fail()` – tariqul anik Jun 05 '20 at 18:15
  • just checked the console. `dd($request)` is null. – tariqul anik Jun 05 '20 at 18:21
1

In your ajax part you are missing error portion

error:function(data)
{
   console.log(data);
}

This is the portion where you will receive laravel validation errors, which will be something like console.log(data.responseJSON.errors) not really sure for this piece of code but you can find in your console. In success method you will never receive validation errors. After that you can play with errors to include in your form inputs

Akhtar Munir
  • 1,691
  • 3
  • 11
  • 28