{"id":129,"date":"2025-08-25T18:14:37","date_gmt":"2025-08-25T18:14:37","guid":{"rendered":"https:\/\/1v0.net\/blog\/?p=129"},"modified":"2025-08-26T08:49:40","modified_gmt":"2025-08-26T08:49:40","slug":"mastering-validation-rules-in-laravel-12","status":"publish","type":"post","link":"https:\/\/1v0.net\/blog\/mastering-validation-rules-in-laravel-12\/","title":{"rendered":"Mastering Validation Rules in Laravel 12"},"content":{"rendered":"\n<p>Validation is one of the most important parts of any Laravel application. It ensures that the data coming from forms, APIs, or user input is safe, correct, and ready to be stored in the database. Without validation, users could submit broken data, leave required fields empty, or even try to exploit your app with malicious input.<\/p>\n\n\n\n<p>In this guide, we\u2019ll go deep into mastering validation rules in Laravel 12. We\u2019ll look at basic validation, custom error messages, form request classes, and some of the most useful built-in rules. Each example is explained in detail so new developers can follow along easily.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1 &#8211; Basic Validation with the <code>validate()<\/code> Method<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Http\/Controllers\/UserController.php<\/span>\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">store<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n<\/span>{\n    $validated = $request-&gt;validate(&#91;\n        <span class=\"hljs-string\">'name'<\/span>  =&gt; <span class=\"hljs-string\">'required|string|max:255'<\/span>,\n        <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required|email|unique:users,email'<\/span>,\n        <span class=\"hljs-string\">'age'<\/span>   =&gt; <span class=\"hljs-string\">'nullable|integer|min:18'<\/span>,\n    ]);\n\n    <span class=\"hljs-comment\">\/\/ At this point $validated contains only valid, safe data<\/span>\n    User::create($validated);\n\n    <span class=\"hljs-keyword\">return<\/span> redirect()-&gt;back()-&gt;with(<span class=\"hljs-string\">'status'<\/span>, <span class=\"hljs-string\">'User created successfully!'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here we use <code>$request-&gt;validate()<\/code> inside a controller method. Laravel automatically checks the input against the rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>required<\/code> means the field must be present and not empty.<\/li>\n\n\n\n<li><code>string<\/code> ensures the name is plain text, not an array or number.<\/li>\n\n\n\n<li><code>max:255<\/code> limits the length of the name field to 255 characters.<\/li>\n\n\n\n<li><code>email<\/code> ensures the input looks like an email address.<\/li>\n\n\n\n<li><code>unique:users,email<\/code> checks the email doesn\u2019t already exist in the <code>users<\/code> table.<\/li>\n\n\n\n<li><code>nullable|integer|min:18<\/code> means age is optional, but if provided it must be an integer at least 18.<\/li>\n<\/ul>\n\n\n\n<p>If validation fails, Laravel will automatically redirect back to the previous page with errors stored in the session. You can display them in Blade using <code>@error<\/code>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2 &#8211; Displaying Validation Errors in Blade<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ resources\/views\/users\/create.blade.php<\/span>\n\n&lt;form method=<span class=\"hljs-string\">\"POST\"<\/span> action=<span class=\"hljs-string\">\"{{ route('users.store') }}\"<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">card<\/span> <span class=\"hljs-title\">card<\/span>-<span class=\"hljs-title\">body<\/span>\"&gt;\n    @<span class=\"hljs-title\">csrf<\/span>\n\n    &lt;<span class=\"hljs-title\">input<\/span> <span class=\"hljs-title\">type<\/span>=\"<span class=\"hljs-title\">text<\/span>\" <span class=\"hljs-title\">name<\/span>=\"<span class=\"hljs-title\">name<\/span>\" <span class=\"hljs-title\">placeholder<\/span>=\"<span class=\"hljs-title\">Name<\/span>\" <span class=\"hljs-title\">value<\/span>=\"<\/span>{{ old(<span class=\"hljs-string\">'name'<\/span>) }}<span class=\"hljs-string\">\" class=\"<\/span>form-control mb<span class=\"hljs-number\">-2<\/span><span class=\"hljs-string\">\"&gt;\n    @error('name')\n        &lt;div class=\"<\/span>text-danger<span class=\"hljs-string\">\"&gt;{{ $message }}&lt;\/div&gt;\n    @enderror\n\n    &lt;input type=\"<\/span>email<span class=\"hljs-string\">\" name=\"<\/span>email<span class=\"hljs-string\">\" placeholder=\"<\/span>Email<span class=\"hljs-string\">\" value=\"<\/span>{{ old(<span class=\"hljs-string\">'email'<\/span>) }}<span class=\"hljs-string\">\" class=\"<\/span>form-control mb<span class=\"hljs-number\">-2<\/span><span class=\"hljs-string\">\"&gt;\n    @error('email')\n        &lt;div class=\"<\/span>text-danger<span class=\"hljs-string\">\"&gt;{{ $message }}&lt;\/div&gt;\n    @enderror\n\n    &lt;input type=\"<\/span>number<span class=\"hljs-string\">\" name=\"<\/span>age<span class=\"hljs-string\">\" placeholder=\"<\/span>Age<span class=\"hljs-string\">\" value=\"<\/span>{{ old(<span class=\"hljs-string\">'age'<\/span>) }}<span class=\"hljs-string\">\" class=\"<\/span>form-control mb<span class=\"hljs-number\">-2<\/span><span class=\"hljs-string\">\"&gt;\n    @error('age')\n        &lt;div class=\"<\/span>text-danger<span class=\"hljs-string\">\"&gt;{{ $message }}&lt;\/div&gt;\n    @enderror\n\n    &lt;button class=\"<\/span>btn btn-primary<span class=\"hljs-string\">\"&gt;Create User&lt;\/button&gt;\n&lt;\/form&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>old()<\/code> helper keeps the previous value if the form fails validation. The <code>@error<\/code> directive shows the error message for each field if validation fails.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3 &#8211; Commonly Used Validation Rules<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>required<\/code> \u2014 Field must be present and not empty.<\/li>\n\n\n\n<li><code>nullable<\/code> \u2014 Field can be empty, but if present, it must follow other rules.<\/li>\n\n\n\n<li><code>email<\/code> \u2014 Must be a valid email format.<\/li>\n\n\n\n<li><code>url<\/code> \u2014 Must be a valid URL.<\/li>\n\n\n\n<li><code>min:n<\/code> \/ <code>max:n<\/code> \u2014 Minimum or maximum length\/size.<\/li>\n\n\n\n<li><code>numeric<\/code> \/ <code>integer<\/code> \u2014 Value must be a number.<\/li>\n\n\n\n<li><code>date<\/code> \u2014 Must be a valid date (Y-m-d, etc.).<\/li>\n\n\n\n<li><code>confirmed<\/code> \u2014 Requires a matching <code>_confirmation<\/code> field (commonly used for passwords).<\/li>\n\n\n\n<li><code>unique:table,column<\/code> \u2014 Ensures no duplicates in the database.<\/li>\n\n\n\n<li><code>exists:table,column<\/code> \u2014 Ensures the value exists in another table (useful for foreign keys).<\/li>\n<\/ul>\n\n\n\n<p>Laravel provides dozens of validation rules out of the box. These rules are simple strings, but you can also pass arrays if you need more flexibility.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4 &#8211; Custom Error Messages<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Http\/Controllers\/UserController.php<\/span>\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">store<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n<\/span>{\n    $messages = &#91;\n        <span class=\"hljs-string\">'name.required'<\/span> =&gt; <span class=\"hljs-string\">'Please enter your full name.'<\/span>,\n        <span class=\"hljs-string\">'email.required'<\/span> =&gt; <span class=\"hljs-string\">'We need your email address.'<\/span>,\n        <span class=\"hljs-string\">'email.unique'<\/span>  =&gt; <span class=\"hljs-string\">'That email is already taken, please choose another.'<\/span>,\n    ];\n\n    $validated = $request-&gt;validate(&#91;\n        <span class=\"hljs-string\">'name'<\/span>  =&gt; <span class=\"hljs-string\">'required|string|max:255'<\/span>,\n        <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required|email|unique:users,email'<\/span>,\n        <span class=\"hljs-string\">'age'<\/span>   =&gt; <span class=\"hljs-string\">'nullable|integer|min:18'<\/span>,\n    ], $messages);\n\n    User::create($validated);\n\n    <span class=\"hljs-keyword\">return<\/span> redirect()-&gt;back()-&gt;with(<span class=\"hljs-string\">'status'<\/span>, <span class=\"hljs-string\">'User created successfully!'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here we pass a second argument to <code>validate()<\/code> with an array of messages. This overrides Laravel\u2019s default messages with custom, user-friendly text.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 &#8211; Using Form Request Classes<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">php<\/span> <span class=\"hljs-selector-tag\">artisan<\/span> <span class=\"hljs-selector-tag\">make<\/span><span class=\"hljs-selector-pseudo\">:request<\/span> <span class=\"hljs-selector-tag\">StoreUserRequest<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This command creates a new class in <code>app\/Http\/Requests\/StoreUserRequest.php<\/code>. Inside it, you define validation rules and messages:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Requests<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Foundation<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">FormRequest<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">StoreUserRequest<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">FormRequest<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">authorize<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">bool<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>; <span class=\"hljs-comment\">\/\/ allow all for now<\/span>\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">rules<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">array<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> &#91;\n            <span class=\"hljs-string\">'name'<\/span>  =&gt; <span class=\"hljs-string\">'required|string|max:255'<\/span>,\n            <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required|email|unique:users,email'<\/span>,\n            <span class=\"hljs-string\">'age'<\/span>   =&gt; <span class=\"hljs-string\">'nullable|integer|min:18'<\/span>,\n        ];\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">messages<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">array<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> &#91;\n            <span class=\"hljs-string\">'name.required'<\/span> =&gt; <span class=\"hljs-string\">'Your name is required.'<\/span>,\n            <span class=\"hljs-string\">'email.unique'<\/span>  =&gt; <span class=\"hljs-string\">'This email is already registered.'<\/span>,\n        ];\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Form requests separate validation logic from controllers. You can now type-hint this request class in your controller:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Http\/Controllers\/UserController.php<\/span>\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">store<\/span><span class=\"hljs-params\">(StoreUserRequest $request)<\/span>\n<\/span>{\n    User::create($request-&gt;validated());\n    <span class=\"hljs-keyword\">return<\/span> redirect()-&gt;back()-&gt;with(<span class=\"hljs-string\">'status'<\/span>, <span class=\"hljs-string\">'User created!'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This makes your controllers cleaner and your validation rules reusable across multiple places.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6 &#8211; Conditional Validation<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ Example: phone is required only if contact_method = phone<\/span>\n\n$request-&gt;validate(&#91;\n    <span class=\"hljs-string\">'contact_method'<\/span> =&gt; <span class=\"hljs-string\">'required|in:email,phone'<\/span>,\n    <span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'required_if:contact_method,email|email'<\/span>,\n    <span class=\"hljs-string\">'phone'<\/span> =&gt; <span class=\"hljs-string\">'required_if:contact_method,phone|digits:10'<\/span>,\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, <code>required_if<\/code> makes the <code>email<\/code> field required if <code>contact_method<\/code> is set to \u201cemail\u201d, and the <code>phone<\/code> field required if \u201cphone\u201d is selected. This gives you dynamic validation depending on user choices.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7 &#8211; Custom Validation Rules<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">php<\/span> <span class=\"hljs-selector-tag\">artisan<\/span> <span class=\"hljs-selector-tag\">make<\/span><span class=\"hljs-selector-pseudo\">:rule<\/span> <span class=\"hljs-selector-tag\">Uppercase<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This generates a rule class. Inside you define your custom logic:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Rules\/Uppercase.php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Rules<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Closure<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Contracts<\/span>\\<span class=\"hljs-title\">Validation<\/span>\\<span class=\"hljs-title\">ValidationRule<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Uppercase<\/span> <span class=\"hljs-keyword\">implements<\/span> <span class=\"hljs-title\">ValidationRule<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">validate<\/span><span class=\"hljs-params\">(string $attribute, mixed $value, Closure $fail)<\/span>: <span class=\"hljs-title\">void<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (strtoupper($value) !== $value) {\n            $fail(<span class=\"hljs-string\">'The :attribute must be uppercase.'<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now you can use it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$request-&gt;validate(&#91;\n    <span class=\"hljs-string\">'code'<\/span> =&gt; &#91;<span class=\"hljs-string\">'required'<\/span>, <span class=\"hljs-keyword\">new<\/span> \\App\\Rules\\Uppercase],\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This ensures the <code>code<\/code> field is always uppercase. Custom rules let you handle business logic that built-in rules don\u2019t cover.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Wrapping Up<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You now have a strong understanding of validation in Laravel 12: from basic rules and error messages to form requests and custom validators. Validation is a core part of keeping your app secure and reliable, and Laravel makes it both powerful and easy to use.<\/p>\n\n\n\n<p>As you build bigger apps, you\u2019ll often combine multiple rules, create reusable form request classes, and even add custom validation rules specific to your project\u2019s business logic.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Next Steps<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Once you\u2019re comfortable with the basics of Laravel validation, here are some advanced topics you can explore to take your skills further:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>File Upload Validation:<\/strong> Learn to validate file types, sizes, and dimensions (e.g., images, PDFs).<\/li>\n\n\n\n<li><strong>Regex Rules:<\/strong> Use regular expressions for highly customized validation logic.<\/li>\n\n\n\n<li><strong>Sanitizing Input:<\/strong> Combine validation with data cleaning (like trimming whitespace, escaping HTML).<\/li>\n\n\n\n<li><strong>Cross-Field Validation:<\/strong> Write rules that compare multiple fields (e.g., start_date must be before end_date).<\/li>\n\n\n\n<li><strong>API Validation:<\/strong> Apply validation rules in API controllers and return JSON error responses.<\/li>\n\n\n\n<li><strong>Localization:<\/strong> Provide validation error messages in multiple languages for international apps.<\/li>\n<\/ul>\n\n\n\n<p>These next steps will help you build more robust, secure, and user-friendly applications where data is always validated properly before saving.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Validation is one of the most important parts of any Laravel application. It ensures that the data coming from forms, APIs, or user input is safe, correct, and ready to be stored in the database. Without validation, users could submit broken data, leave required fields empty, or even try to exploit your app with malicious [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[19],"class_list":["post-129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-validation"],"_links":{"self":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":2,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions\/132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/media\/131"}],"wp:attachment":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}