tariqul anik
tariqul anik

Reputation: 324

Laravel Validation via Ajax: How to show error message in blade

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.

Upvotes: 1

Views: 8522

Answers (3)

dogakorkmaz
dogakorkmaz

Reputation: 129

There is no ajax in this answer, but if anyone wonders how to get laravel error/success sessions with javascript, here is a hack

@if (session('success'))
<div class="form-success text-white" hidden aria-hidden="true">
    <div class="absolute">
        {{ session('success') }}
    </div>
</div>
@endif

and in javascript

document.addEventListener("DOMContentLoaded", function () {
    let FormSuccess = document.querySelector(".form-success");
    if (FormSuccess) {
        Swal.fire({
            icon: "success",
            title: "Successful",
            text: FormSuccess.textContent,
        });
    }
});

then in your controller or service just do

return redirect()->route("name")->with("register_success","Registered succesfully!");

Upvotes: 1

Akhtar Munir
Akhtar Munir

Reputation: 1769

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

Upvotes: 1

STA
STA

Reputation: 34668

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
    }

Upvotes: 1

Related Questions