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=".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.
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./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.
-
This is some link that could help you: stackoverflow./questions/10931836/… Modifiying the way you do your ajax request and using the
.fail()
will ease your way to display errors. – ettdro Commented Jun 5, 2020 at 17:24 -
Return output from
ajaxValidation
via json. And catch it with ajax and show the error – STA Commented Jun 5, 2020 at 17:39 - how can I return errors from validate($request)? Can you please tell me? @TalhaF. – tariqul anik Commented Jun 5, 2020 at 17:42
-
Please provide your JSON response code from
ajaxValidation
– STA Commented Jun 5, 2020 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 Commented Jun 5, 2020 at 17:47
3 Answers
Reset to default 1In 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
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
}
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!");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745416597a4626778.html
评论列表(0条)