Laravel Validation via Ajax: How to show error message in blade
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Validation via Ajax: How to Show Error Messages in Blade
As a senior developer, I often encounter scenarios where we need dynamic, real-time feedback for form validation without forcing a full page reload. AJAX is the perfect tool for this, especially when dealing with user input like checking if a NID number already exists.
The challenge you are facing—how to correctly propagate validation errors from a server-side response back into your Blade view after an AJAX request—is common. The confusion often stems from how Laravel handles redirects versus JSON responses.
This guide will walk you through the correct architecture for implementing AJAX validation in a Laravel application, ensuring that error messages displayed in your Blade template are accurate and context-aware. We will look at your provided code snippets and refine the process into a robust solution.
Understanding the Frontend Setup (Blade & jQuery)
Your setup for displaying errors in the Blade file is excellent. It correctly uses Laravel's $errors bag to conditionally display feedback:
<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>
This structure is designed to display errors after a standard form submission. The key difference in an AJAX context is that the data returned from the server must contain these error messages, not just a success/failure flag.
The Server-Side Challenge: Handling Validation in AJAX
Your instinct about $this->validate() returning errors automatically is correct for traditional web requests (where the framework handles redirects). However, when dealing with an API or AJAX endpoint, you need to explicitly control the response format.
When a validation fails on an AJAX request, instead of letting Laravel redirect, you must manually construct a JSON response that clearly indicates success or failure and includes any associated error messages. This is a fundamental pattern in building APIs with Laravel.
Correcting the Controller Logic
Instead of relying solely on $this->validate() within your AJAX endpoint, we will handle the validation explicitly and return structured data. Note that for true real-time keyup validation, you often check the uniqueness directly in the request handler.
Here is how you should structure your controller method:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator; // Good practice to use Validator facade
public function ajaxValidation(Request $request)
{
// 1. Define the rules we are checking (e.g., uniqueness check)
$rules = [
'nid_number' => 'unique:members',
];
// 2. Validate the incoming request data
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
// Validation failed: Return an error response
return response()->json([
'success' => false,
'errors' => $validator->errors() // This array contains the error messages
], 422); // Use HTTP status code 422 Unprocessable Entity for validation errors
}
// Validation succeeded: Return success response
return response()->json([
'success' => true,
'message' => 'NID number is available.'
]);
}
Why this approach works: By manually checking the validation result and returning a JSON object containing an errors array upon failure (using the standard HTTP status code 422), you give your JavaScript code everything it needs to handle the feedback dynamically. This pattern aligns perfectly with building robust APIs, which is a core focus of frameworks like Laravel.
The Frontend AJAX Implementation
Now, we update the jQuery AJAX call to correctly parse the response:
$(document).on('keyup', '#nid', function() {
$.ajax({
url: "{{ route('ajax-validation') }}",
method: 'POST',
data: { query: $(this).val() },
dataType: 'json', // Expect JSON response
success: function(data) {
if (data.success) {
// Handle success (e.g., clear previous error state if necessary)
console.log('Validation successful.');
} else {
// Handle failure: Display the specific errors received from the server
console.error('Validation failed:', data.errors);
// You would typically call a function here to dynamically insert these messages into the form context.
}
},
error: function(xhr, status, error) {
// Handle network or server errors
console.error('AJAX Error:', error);
}
});
});
Conclusion
Showing validation errors via AJAX requires shifting focus from traditional form submission handling to API response management. You do not need Laravel's built-in validate() method for this specific use case, as it is designed for full-page redirects. Instead, by manually using the Validator facade in your controller and returning a structured JSON response with an appropriate HTTP status code (like 422), you gain complete control over the user experience. This technique ensures that your frontend correctly interprets the server-side errors, leading to cleaner, more interactive applications, keeping you on the path of modern Laravel development.