All packages
Options for Laravel
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:
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:
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'.
- 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 cryptoBy donating, you agree to our donation terms.
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.