---
title: "Validation — Enums for Laravel | Roundly"
description: "validationRule() builds a Laravel in: rule from values(), so the allow-list never drifts when cases change."
url: https://roundly-consulting.com/open-source/docs/enums-for-laravel/validation
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Enums for Laravel](https://roundly-consulting.com/open-source/docs/enums-for-laravel.md)

# Validation

validationRule() builds a Laravel in:... rule string from values() — the backed values, or the case names on a pure enum — so the allow-list never drifts when cases change:

```php
$request->validate([
    'category' => ['required', NewsCategory::validationRule()],
]);
// validationRule() === 'in:hot-news,regular-news,private-news'
```

## In a form request

```php
namespace App\Http\Requests;

use App\Enums\NewsCategory;
use Illuminate\Foundation\Http\FormRequest;

final class StoreArticleRequest extends FormRequest
{
    /**
     * @return array<string, array<int, string>>
     */
    public function rules(): array
    {
        return [
            'title' => ['required', 'string', 'max:255'],
            'category' => ['required', NewsCategory::validationRule()],
        ];
    }
}
```

Once validated, the value is known to belong to a case, so PHP’s native from() can’t throw:

```php
public function store(StoreArticleRequest $request): RedirectResponse
{
    $article = Article::create([
        'title' => $request->validated('title'),
        'category' => NewsCategory::from($request->validated('category')),
    ]);

    return to_route('articles.show', $article);
}
```

## Arrays of values

For a list of values, apply the rule to every element:

```php
$request->validate([
    'categories' => ['required', 'array'],
    'categories.*' => [NewsCategory::validationRule()],
]);
```

## What goes into the rule

- The rule is in: plus values() joined with commas — 'in:0,5,10' for an int-backed enum. Laravel’s in rule compares as strings, so form input like '5' passes.
- On a pure enum it’s built from the case names — 'in:Active,Archived' — and accepts exactly those.
- Laravel reads the list back as CSV, so a value that would break that syntax — an empty one, one containing a comma, or one starting with a double quote — is quoted with its inner quotes doubled. Every other value stays bare.
- Pass the rule as an array element, as above, rather than inside a |-delimited rule string.

```php
use RoundlyConsulting\Enums\Helpers;

enum Separator: string
{
    use Helpers;

    case Comma = 'a,b';
    case Plain = 'c';
}

Separator::validationRule(); // 'in:"a,b",c'
```

## Values with backslashes

Inside quotes Laravel’s CSV parser keeps a backslash and the character after it verbatim, so a quoted value holding a backslash is written to match: a quote right after a backslash is not doubled, and a trailing run of backslashes goes just after the closing quote, where the parser still adds it to the value:

```php
enum Fragment: string
{
    use Helpers;

    case TrailingBackslash = 'C:\a,b\\';   // C:\a,b\
    case EscapedQuote = 'x\"y,z';          // x\"y,z
    case Plain = 'z';
}

Fragment::validationRule(); // 'in:"C:\a,b"\,"x\"y,z",z'
```

Each value is checked to read back as itself. If one ever didn’t, validationRule() would throw EnumException::valueNotRepresentable() rather than return a rule that accepts the wrong input. No such value is known on PHP 8.4 — validate that enum with Rule::enum() if it happens.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/enums-for-laravel.md)
