php
45 lines · 6 steps
How a Laravel FormRequest validates invoices
A FormRequest bundles authorization, validation rules, and localized error messaging into one reusable class.
Explained by
highlit
1<?php
2
3namespace App\Http\Requests;
4
5use Illuminate\Foundation\Http\FormRequest;
6
7class StoreInvoiceRequest extends FormRequest
8{
9 public function authorize(): bool
10 {
11 return $this->user()->can('create', Invoice::class);
12 }
13
14 public function rules(): array
15 {
16 return [
17 'customer_email' => ['required', 'email', 'max:255'],
18 'issued_at' => ['required', 'date', 'before_or_equal:today'],
19 'due_at' => ['required', 'date', 'after:issued_at'],
20 'line_items' => ['required', 'array', 'min:1'],
21 'line_items.*.description' => ['required', 'string', 'max:500'],
22 'line_items.*.amount' => ['required', 'numeric', 'min:0.01'],
23 ];
24 }
25
26 public function messages(): array
27 {
28 return [
29 'due_at.after' => __('validation.custom.invoice.due_after_issue'),
30 'line_items.min' => __('validation.custom.invoice.line_items_required'),
31 'line_items.*.amount.min' => __('validation.custom.invoice.positive_amount'),
32 ];
33 }
34
35 public function attributes(): array
36 {
37 return [
38 'customer_email' => __('validation.attributes.customer_email'),
39 'issued_at' => __('validation.attributes.issued_at'),
40 'due_at' => __('validation.attributes.due_at'),
41 'line_items.*.description' => __('validation.attributes.line_item_description'),
42 'line_items.*.amount' => __('validation.attributes.line_item_amount'),
43 ];
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1FormRequest classes keep authorization and validation out of controllers so actions stay lean.
- 2Dot and star notation let you validate every element of a nested array with a single rule key.
- 3Overriding messages() and attributes() turns raw field names into human-friendly, translatable error text.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-laravel-formrequest-validates-invoices-explained-php-fe52/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.