Enhancing Numeric Validation with Laravel's Fluent Rule Interface
- Laravel
Laravel introduces a more expressive approach to numeric validation through the fluent Rule::numeric() interface. This syntax transforms traditional string-based rules into chainable methods for improved readability.
The implementation provides a more developer-friendly syntax:
1$validated = $request->validate([ 2 'name' => 'required|string|max:255', 3 'price' => Rule::numeric() 4 ->min(0.01) 5 ->max(9999.99) 6 ->decimal(2), 7 'weight' => Rule::numeric() 8 ->min(0) 9 ->decimal(3)10 ->nullable(),11 'stock' => Rule::numeric()12 ->integer()13 ->min(0)14 ->nullable(),15 'discount_percent' => Rule::numeric()16 ->between(0, 100)17 ->decimal(1)18]);