Laravel 5 validation in model
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Validation in Laravel Models: From Rules to Readable Errors
As developers working with Laravel, managing data integrity through validation is fundamental. We often separate concerns by defining validation rules within the Model itself, and then executing that logic in the Controller. However, when it comes to presenting those validation errors back to the user, developers frequently run into issues where the default output isn't as clear or grouped as desired.
This post will dive deep into how validation works within the Laravel ecosystem, examine the setup you described, and provide robust solutions for displaying comprehensive, user-friendly error messages.
## The Anatomy of Model Validation
The setup you presented is a common pattern: defining rules in the model and executing validation in the controller.
```php
// Model Example
class Test extends Model
{
public $rules = [
'title' => 'required',
'name' => 'required',
];
protected $fillable = ['title','name'];
}
```
This approach is clean for defining *what* data is required, which adheres to the principle of separation of concerns. However, when you manually trigger validation in the controller using `Validator::make()`, Laravel generates errors based on the fields that failed. The default output, like `"The 0 field is required."`, is technically correct but lacks the contextual grouping you are aiming for.
## Why Default Errors Fall Short
The issue arises because the standard error reporting mechanism focuses on individual field failures rather than providing a holistic summary of all outstanding errors. When multiple fields fail validation simultaneously, the resulting output can look fragmented, making it difficult for the end-user to quickly identify all necessary corrections at once.
You want to move from:
> The name field is required. (Error 1)
> The title field is required. (Error 2)
To a consolidated view:
> The name field is required.
> The title field is required.
Achieving this requires stepping beyond the basic validation execution and manipulating the resulting error messages before they are rendered.
## Solution: Customizing Error Messages for Better UX
Since Laravel's built-in validator doesn't automatically group these errors into a single message, we need to handle the retrieval of the error data manually. This is especially true when you bypass the standard Form Request validation in favor of manual handling.
Here is how you can achieve your desired grouped output within your controller logic:
```php
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
public function store(Request $request)
{
$test = new Test;
// 1. Run the validation
$validator = Validator::make($request->all(), [
'title' => 'required',
'name' => 'required',
]);
if ($validator->fails()) {
// 2. Retrieve and format the errors manually
$errors = $validator->errors();
$formattedErrors = [];
// Iterate through the errors to create a custom, grouped message
foreach ($errors->all() as $field => $messages) {
if (in_array($field, ['title', 'name'])) {
$formattedErrors[] = "The {$field} field is required.";
}
}
// 3. Return the view with the custom messages
return view('test', ['errors' => $formattedErrors]);
}
// If validation passes, proceed with creation
$test->create($request->all());
}
```
### Best Practice: Embrace Form Requests
While the manual method above solves your immediate display problem, for larger applications, we strongly recommend leveraging Laravel’s built-in **Form Request** system. As discussed on the official documentation from [laravelcompany.com](https://