---
title: "Validation — Options for Laravel | Roundly"
description: "Return Laravel validation rules from rules() and every write is validated before it’s cast or stored."
url: https://roundly-consulting.com/open-source/docs/options-for-laravel/validation
language: en
---

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

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

# Validation

Return Laravel validation rules from rules() to validate on write. The default is an empty array, so nothing is validated until you opt in:

```php
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use RoundlyConsulting\Options\BaseOption;

final class SeatLimitOption extends BaseOption
{
    public function key(): string
    {
        return 'seat-limit';
    }

    public function default(): mixed
    {
        return 5;
    }

    public function castAs(): string|CastsAttributes
    {
        return 'integer';
    }

    public function rules(): array|string
    {
        return ['required', 'integer', 'min:1', 'max:100'];
    }
}
```

Rules are applied to a field named value, before the cast and before anything is stored. Invalid values throw Illuminate\\Validation\\ValidationException — Laravel’s own exception, not an OptionException, so a controller turns it into the usual 422 or redirect with errors:

```php
use Illuminate\Validation\ValidationException;

try {
    Options::set(SeatLimitOption::class, 250, $team);
} catch (ValidationException $e) {
    $e->errors(); // ['value' => ['…']] — rules are applied to a field named "value"
}
```

## Where it applies

- Every write that goes through set(): the facade, the fluent API, helpers, HasOptions, setMany(), remember() when it stores, group set() and options:set.
- setMany() and group set() validate the whole batch before the first write — one invalid value means nothing is stored.
- Rules may be an array or a pipe-delimited string such as 'integer|min:0'.
- Laravel skips non-implicit rules such as in: for a blank string, so \['in:light,dark'\] alone lets '' through. Add required — or nullable, if blank is meant to be allowed.
- Imports write raw rows and don’t run rules(). Options::fake() validates like the real manager — an invalid write throws and isn’t recorded.

## 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/options-for-laravel.md)
