Validation attributes | laravel-data | Spatie

 SPATIE

  Laravel Data
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-data](https://spatie.be/docs/laravel-data/v3)  Advanced-usage  Validation attributes

 Version   v4   v3   v2   v1

 Other versions for crawler [v4](https://spatie.be/docs/laravel-data/v4) [v3](https://spatie.be/docs/laravel-data/v3) [v2](https://spatie.be/docs/laravel-data/v2) [v1](https://spatie.be/docs/laravel-data/v1)

- [ Introduction ](https://spatie.be/docs/laravel-data/v3/introduction)
- [ Support us ](https://spatie.be/docs/laravel-data/v3/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-data/v3/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-data/v3/installation-setup)
- [ Third party packages ](https://spatie.be/docs/laravel-data/v3/third-party-packages)
- [ Questions and issues ](https://spatie.be/docs/laravel-data/v3/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-data/v3/changelog)
- [ About us ](https://spatie.be/docs/laravel-data/v3/about-us)

Getting started
---------------

- [ Quickstart ](https://spatie.be/docs/laravel-data/v3/getting-started/quickstart)

As a DTO
--------

- [ Creating a data object ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/creating-a-data-object)
- [ Optional properties ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/optional-properties)
- [ Nesting ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/nesting)
- [ Collections ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/collections)
- [ Casts ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/casts)
- [ Default values ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/defaults)
- [ Computed values ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/computed)
- [ From a request ](https://spatie.be/docs/laravel-data/v3/as-a-data-transfer-object/request-to-data-object)

As a resource
-------------

- [ From data to resource ](https://spatie.be/docs/laravel-data/v3/as-a-resource/from-data-to-resource)
- [ Including and excluding properties ](https://spatie.be/docs/laravel-data/v3/as-a-resource/lazy-properties)
- [ Wrapping ](https://spatie.be/docs/laravel-data/v3/as-a-resource/wrapping)
- [ Transforming data ](https://spatie.be/docs/laravel-data/v3/as-a-resource/transformers)

Advanced usage
--------------

- [ Eloquent casting ](https://spatie.be/docs/laravel-data/v3/advanced-usage/eloquent-casting)
- [ Transforming to TypeScript ](https://spatie.be/docs/laravel-data/v3/advanced-usage/typescript)
- [ Working with dates ](https://spatie.be/docs/laravel-data/v3/advanced-usage/working-with-dates)
- [ Normalizers ](https://spatie.be/docs/laravel-data/v3/advanced-usage/normalizers)
- [ Pipeline ](https://spatie.be/docs/laravel-data/v3/advanced-usage/pipeline)
- [ Use with Inertia ](https://spatie.be/docs/laravel-data/v3/advanced-usage/use-with-inertia)
- [ Use with Livewire ](https://spatie.be/docs/laravel-data/v3/advanced-usage/use-with-livewire)
- [ Creating a cast ](https://spatie.be/docs/laravel-data/v3/advanced-usage/creating-a-cast)
- [ Creating a transformer ](https://spatie.be/docs/laravel-data/v3/advanced-usage/creating-a-transformer)
- [ Filling properties from route parameters ](https://spatie.be/docs/laravel-data/v3/advanced-usage/filling%20from-route-parameters)
- [ Creating a rule inferrer ](https://spatie.be/docs/laravel-data/v3/advanced-usage/creating-a-rule-inferrer)
- [ Internal structures ](https://spatie.be/docs/laravel-data/v3/advanced-usage/internal-structures)
- [ Custom collections ](https://spatie.be/docs/laravel-data/v3/advanced-usage/custom-collections)
- [ Mapping rules ](https://spatie.be/docs/laravel-data/v3/advanced-usage/mapping-rules)
- [ Validation attributes ](https://spatie.be/docs/laravel-data/v3/advanced-usage/validation-attributes)
- [ Performance ](https://spatie.be/docs/laravel-data/v3/advanced-usage/performance)
- [ Commands ](https://spatie.be/docs/laravel-data/v3/advanced-usage/commands)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-data                                                                                                                                                                                                                                    `

Validation attributes
=====================

###  On this page

1. [ Creating your validation attribute ](#content-creating-your-validation-attribute)
2. [ Available validation attributes ](#content-available-validation-attributes)

It is possible to validate the request before a data object is constructed. This can be done by adding validation attributes to the properties of a data object like this:

```
class SongData extends Data
{
    public function __construct(
        #[Uuid()]
        public string $uuid,
        #[Max(15), IP, StartsWith('192.')]
        public string $ip,
    ) {
    }
}
```

Creating your validation attribute
--------------------------------------------------------------------------------------------------------------------------------------------------------------

It is possible to create your own validation attribute by extending the `CustomValidationAttribute` class, this class has a `getRules` method that returns the rules that should be applied to the property.

```
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class CustomRule extends CustomValidationAttribute
{
    /**
     * @return array|object|string
     */
    public function getRules(ValidationPath $path): array|object|string;
    {
        return [new CustomRule()];
    }
}
```

Quick note: you can only use these rules as an attribute, not as a class rule within the static `rules` method of the data class.

Available validation attributes
-----------------------------------------------------------------------------------------------------------------------------------------------------

### Accepted

[Docs](https://laravel.com/docs/9.x/validation#rule-accepted)

```
#[Accepted]
public bool $closure;
```

### AcceptedIf

[Docs](https://laravel.com/docs/9.x/validation#rule-accepted-if)

```
#[AcceptedIf('other_field', 'equals_this')]
public bool $closure;
```

### ActiveUrl

[Docs](https://laravel.com/docs/9.x/validation#rule-active-url)

```
#[ActiveUrl]
public string $closure;
```

### After

[Docs](https://laravel.com/docs/9.x/validation#rule-after)

```
#[After('tomorrow')]
public Carbon $closure;

#[After(Carbon::yesterday())]
public Carbon $closure;

// Always use field references when referencing other fields
#[After(new FieldReference('other_field'))]
public Carbon $closure;
```

### AfterOrEqual

[Docs](https://laravel.com/docs/9.x/validation#rule-after-or-equal)

```
#[AfterOrEqual('tomorrow')]
public Carbon $closure;

#[AfterOrEqual(Carbon::yesterday())]
public Carbon $closure;

// Always use field references when referencing other fields
#[AfterOrEqual(new FieldReference('other_field'))]
public Carbon $closure;
```

### Alpha

[Docs](https://laravel.com/docs/9.x/validation#rule-alpha)

```
#[Alpha]
public string $closure;
```

### AlphaDash

[Docs](https://laravel.com/docs/9.x/validation#rule-alpha-dash)

```
#[AlphaDash]
public string $closure;
```

### AlphaNumeric

[Docs](https://laravel.com/docs/9.x/validation#rule-alpha-num)

```
#[AlphaNumeric]
public string $closure;
```

### ArrayType

[Docs](https://laravel.com/docs/9.x/validation#rule-array)

```
#[ArrayType]
public array $closure;

#[ArrayType(['valid_key', 'other_valid_key'])]
public array $closure;

#[ArrayType('valid_key', 'other_valid_key')]
public array $closure;
```

### Bail

[Docs](https://laravel.com/docs/9.x/validation#rule-bail)

```
#[Bail]
public string $closure;
```

### Before

[Docs](https://laravel.com/docs/9.x/validation#rule-before)

```
#[Before('tomorrow')]
public Carbon $closure;

#[Before(Carbon::yesterday())]
public Carbon $closure;

// Always use field references when referencing other fields
#[Before(new FieldReference('other_field'))]
public Carbon $closure;
```

### BeforeOrEqual

[Docs](https://laravel.com/docs/9.x/validation#rule-before-or-equal)

```
#[BeforeOrEqual('tomorrow')]
public Carbon $closure;

#[BeforeOrEqual(Carbon::yesterday())]
public Carbon $closure;

// Always use field references when referencing other fields
#[BeforeOrEqual(new FieldReference('other_field'))]
public Carbon $closure;
```

### Between

[Docs](https://laravel.com/docs/9.x/validation#rule-between)

```
#[Between(3.14, 42)]
public int $closure;
```

### BooleanType

[Docs](https://laravel.com/docs/9.x/validation#rule-boolean)

```
#[BooleanType]
public bool $closure;
```

### Confirmed

[Docs](https://laravel.com/docs/9.x/validation#rule-confirmed)

```
#[Confirmed]
public string $closure;
```

### CurrentPassword

[Docs](https://laravel.com/docs/9.x/validation#rule-current-password)

```
#[CurrentPassword]
public string $closure;

#[CurrentPassword('api')]
public string $closure;
```

### Date

[Docs](https://laravel.com/docs/9.x/validation#rule-date)

```
#[Date]
public Carbon $closure;
```

### DateEquals

[Docs](https://laravel.com/docs/9.x/validation#rule-date-equals)

```
#[DateEquals('tomorrow')]
public Carbon $closure;

#[DateEquals(Carbon::yesterday())]
public Carbon $closure;
```

### DateFormat

[Docs](https://laravel.com/docs/9.x/validation#rule-date-format)

```
#[DateFormat('d-m-Y')]
public Carbon $closure;
```

### Declined

[Docs](https://laravel.com/docs/9.x/validation#rule-declined)

```
#[Declined]
public bool $closure;
```

### DeclinedIf

[Docs](https://laravel.com/docs/9.x/validation#rule-declined-if)

```
#[DeclinedIf('other_field', 'equals_this')]
public bool $closure;
```

### Different

[Docs](https://laravel.com/docs/9.x/validation#rule-different)

```
#[Different('other_field')]
public string $closure;
```

### Digits

[Docs](https://laravel.com/docs/9.x/validation#rule-digits)

```
#[Digits(10)]
public int $closure;
```

### DigitsBetween

[Docs](https://laravel.com/docs/9.x/validation#rule-digits-between)

```
#[DigitsBetween(2, 10)]
public int $closure;
```

### Dimensions

[Docs](https://laravel.com/docs/9.x/validation#rule-dimensions)

```
#[Dimensions(ratio: 1.5)]
public UploadedFile $closure;

#[Dimensions(maxWidth: 100, maxHeight: 100)]
public UploadedFile $closure;
```

### Distinct

[Docs](https://laravel.com/docs/9.x/validation#rule-distinct)

```
#[Distinct]
public string $closure;

#[Distinct(Distinct::Strict)]
public string $closure;

#[Distinct(Distinct::IgnoreCase)]
public string $closure;
```

### DoesntEndWith

[Docs](https://laravel.com/docs/9.x/validation#rule-doesnt-end-with)

```
#[DoesntEndWith('a')]
public string $closure;

#[DoesntEndWith(['a', 'b'])]
public string $closure;

#[DoesntEndWith('a', 'b')]
public string $closure;
```

### DoesntStartWith

[Docs](https://laravel.com/docs/9.x/validation#rule-doesnt-start-with)

```
#[DoesntStartWith('a')]
public string $closure;

#[DoesntStartWith(['a', 'b'])]
public string $closure;

#[DoesntStartWith('a', 'b')]
public string $closure;
```

### Email

[Docs](https://laravel.com/docs/9.x/validation#rule-email)

```
#[Email]
public string $closure;

#[Email(Email::RfcValidation)]
public string $closure;

#[Email([Email::RfcValidation, Email::DnsCheckValidation])]
public string $closure;

#[Email(Email::RfcValidation, Email::DnsCheckValidation)]
public string $closure;
```

### EndsWith

[Docs](https://laravel.com/docs/9.x/validation#rule-ends-with)

```
#[EndsWith('a')]
public string $closure;

#[EndsWith(['a', 'b'])]
public string $closure;

#[EndsWith('a', 'b')]
public string $closure;
```

### Enum

[Docs](https://laravel.com/docs/9.x/validation#rule-enum)

```
#[Enum(ChannelType::class)]
public string $closure;
```

### ExcludeIf

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*

[Docs](https://laravel.com/docs/9.x/validation#rule-exclude-if)

```
#[ExcludeIf('other_field', 'has_value')]
public string $closure;
```

### ExcludeUnless

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*

[Docs](https://laravel.com/docs/9.x/validation#rule-exclude-unless)

```
#[ExcludeUnless('other_field', 'has_value')]
public string $closure;
```

### ExcludeWith

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*

[Docs](https://laravel.com/docs/9.x/validation#rule-exclude-with)

```
#[ExcludeWith('other_field')]
public string $closure;
```

### ExcludeWithout

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*

[Docs](https://laravel.com/docs/9.x/validation#rule-exclude-without)

```
#[ExcludeWithout('other_field')]
public string $closure;
```

### Exists

[Docs](https://laravel.com/docs/9.x/validation#rule-exists)

```
#[Exists('users')]
public string $closure;

#[Exists(User::class)]
public string $closure;

#[Exists('users', 'email')]
public string $closure;

#[Exists('users', 'email', connection: 'tenant')]
public string $closure;

#[Exists('users', 'email', withoutTrashed: true)]
public string $closure;
```

### File

[Docs](https://laravel.com/docs/9.x/validation#rule-file)

```
#[File]
public UploadedFile $closure;
```

### Filled

[Docs](https://laravel.com/docs/9.x/validation#rule-filled)

```
#[Filled]
public string $closure;
```

### GreaterThan

[Docs](https://laravel.com/docs/9.x/validation#rule-gt)

```
#[GreaterThan('other_field')]
public int $closure;
```

### GreaterThanOrEqualTo

[Docs](https://laravel.com/docs/9.x/validation#rule-gte)

```
#[GreaterThanOrEqualTo('other_field')]
public int $closure;
```

### Image

[Docs](https://laravel.com/docs/9.x/validation#rule-image)

```
#[Image]
public UploadedFile $closure;
```

### In

[Docs](https://laravel.com/docs/9.x/validation#rule-in)

```
#[In([1, 2, 3, 'a', 'b'])]
public mixed $closure;

#[In(1, 2, 3, 'a', 'b')]
public mixed $closure;
```

### InArray

[Docs](https://laravel.com/docs/9.x/validation#rule-in-array)

```
#[InArray('other_field')]
public string $closure;
```

### IntegerType

[Docs](https://laravel.com/docs/9.x/validation#rule-integer)

```
#[IntegerType]
public int $closure;
```

### IP

[Docs](https://laravel.com/docs/9.x/validation#rule-ip)

```
#[IP]
public string $closure;
```

### IPv4

[Docs](https://laravel.com/docs/9.x/validation#ipv4)

```
#[IPv4]
public string $closure;
```

### IPv6

[Docs](https://laravel.com/docs/9.x/validation#ipv6)

```
#[IPv6]
public string $closure;
```

### Json

[Docs](https://laravel.com/docs/9.x/validation#rule-json)

```
#[Json]
public string $closure;
```

### LessThan

[Docs](https://laravel.com/docs/9.x/validation#rule-lt)

```
#[LessThan('other_field')]
public int $closure;
```

### LessThanOrEqualTo

[Docs](https://laravel.com/docs/9.x/validation#rule-lte)

```
#[LessThanOrEqualTo('other_field')]
public int $closure;
```

### Lowercase

[Docs](https://laravel.com/docs/9.x/validation#rule-lowercase)

```
#[Lowercase]
public string $closure;
```

### MacAddress

[Docs](https://laravel.com/docs/9.x/validation#rule-mac)

```
#[MacAddress]
public string $closure;
```

### Max

[Docs](https://laravel.com/docs/9.x/validation#rule-max)

```
#[Max(20)]
public int $closure;
```

### MaxDigits

[Docs](https://laravel.com/docs/9.x/validation#rule-max-digits)

```
#[MaxDigits(10)]
public int $closure;
```

### MimeTypes

[Docs](https://laravel.com/docs/9.x/validation#rule-mimetypes)

```
#[MimeTypes('video/quicktime')]
public UploadedFile $closure;

#[MimeTypes(['video/quicktime', 'video/avi'])]
public UploadedFile $closure;

#[MimeTypes('video/quicktime', 'video/avi')]
public UploadedFile $closure;
```

### Mimes

[Docs](https://laravel.com/docs/9.x/validation#rule-mimes)

```
#[Mimes('jpg')]
public UploadedFile $closure;

#[Mimes(['jpg', 'png'])]
public UploadedFile $closure;

#[Mimes('jpg', 'png')]
public UploadedFile $closure;
```

### Min

[Docs](https://laravel.com/docs/9.x/validation#rule-min)

```
#[Min(20)]
public int $closure;
```

### MinDigits

[Docs](https://laravel.com/docs/9.x/validation#rule-min-digits)

```
#[MinDigits(2)]
public int $closure;
```

### MultipleOf

[Docs](https://laravel.com/docs/9.x/validation#rule-multiple-of)

```
#[MultipleOf(3)]
public int $closure;
```

### NotIn

[Docs](https://laravel.com/docs/9.x/validation#rule-not-in)

```
#[NotIn([1, 2, 3, 'a', 'b'])]
public mixed $closure;

#[NotIn(1, 2, 3, 'a', 'b')]
public mixed $closure;
```

### NotRegex

[Docs](https://laravel.com/docs/9.x/validation#rule-not-regex)

```
#[NotRegex('/^.+$/i')]
public string $closure;
```

### Nullable

[Docs](https://laravel.com/docs/9.x/validation#rule-nullable)

```
#[Nullable]
public ?string $closure;
```

### Numeric

[Docs](https://laravel.com/docs/9.x/validation#rule-numeric)

```
#[Numeric]
public ?string $closure;
```

### Password

[Docs](https://laravel.com/docs/9.x/validation#rule-password)

```
#[Password(min: 12, letters: true, mixedCase: true, numbers: false, symbols: false, uncompromised: true, uncompromisedThreshold: 0)]
public string $closure;
```

### Present

[Docs](https://laravel.com/docs/9.x/validation#rule-present)

```
#[Present]
public string $closure;
```

### Prohibited

[Docs](https://laravel.com/docs/9.x/validation#rule-prohibited)

```
#[Prohibited]
public ?string $closure;
```

### ProhibitedIf

[Docs](https://laravel.com/docs/9.x/validation#rule-prohibited-if)

```
#[ProhibitedIf('other_field', 'has_value')]
public ?string $closure;

#[ProhibitedIf('other_field', ['has_value', 'or_this_value'])]
public ?string $closure;
```

### ProhibitedUnless

[Docs](https://laravel.com/docs/9.x/validation#rule-prohibited-unless)

```
#[ProhibitedUnless('other_field', 'has_value')]
public ?string $closure;

#[ProhibitedUnless('other_field', ['has_value', 'or_this_value'])]
public ?string $closure;
```

### Prohibits

[Docs](https://laravel.com/docs/9.x/validation#rule-prohibits)

```
#[Prohibits('other_field')]
public ?string $closure;

#[Prohibits(['other_field', 'another_field'])]
public ?string $closure;

#[Prohibits('other_field', 'another_field')]
public ?string $closure;
```

### Regex

[Docs](https://laravel.com/docs/9.x/validation#rule-regex)

```
#[Regex('/^.+$/i')]
public string $closure;
```

### Required

[Docs](https://laravel.com/docs/9.x/validation#rule-required)

```
#[Required]
public string $closure;
```

### RequiredIf

[Docs](https://laravel.com/docs/9.x/validation#rule-required-if)

```
#[RequiredIf('other_field', 'value')]
public ?string $closure;

#[RequiredIf('other_field', ['value', 'another_value'])]
public ?string $closure;
```

### RequiredUnless

[Docs](https://laravel.com/docs/9.x/validation#rule-required-unless)

```
#[RequiredUnless('other_field', 'value')]
public ?string $closure;

#[RequiredUnless('other_field', ['value', 'another_value'])]
public ?string $closure;
```

### RequiredWith

[Docs](https://laravel.com/docs/9.x/validation#rule-required-with)

```
#[RequiredWith('other_field')]
public ?string $closure;

#[RequiredWith(['other_field', 'another_field'])]
public ?string $closure;

#[RequiredWith('other_field', 'another_field')]
public ?string $closure;
```

### RequiredWithAll

[Docs](https://laravel.com/docs/9.x/validation#rule-required-with-all)

```
#[RequiredWithAll('other_field')]
public ?string $closure;

#[RequiredWithAll(['other_field', 'another_field'])]
public ?string $closure;

#[RequiredWithAll('other_field', 'another_field')]
public ?string $closure;
```

### RequiredWithout

[Docs](https://laravel.com/docs/9.x/validation#rule-required-without)

```
#[RequiredWithout('other_field')]
public ?string $closure;

#[RequiredWithout(['other_field', 'another_field'])]
public ?string $closure;

#[RequiredWithout('other_field', 'another_field')]
public ?string $closure;
```

### RequiredWithoutAll

[Docs](https://laravel.com/docs/9.x/validation#rule-required-without-all)

```
#[RequiredWithoutAll('other_field')]
public ?string $closure;

#[RequiredWithoutAll(['other_field', 'another_field'])]
public ?string $closure;

#[RequiredWithoutAll('other_field', 'another_field')]
public ?string $closure;
```

### RequiredArrayKeys

[Docs](https://laravel.com/docs/9.x/validation#rule-required-array-keys)

```
#[RequiredArrayKeys('a')]
public array $closure;

#[RequiredArrayKeys(['a', 'b'])]
public array $closure;

#[RequiredArrayKeys('a', 'b')]
public array $closure;
```

### Rule

```
#[Rule('string|uuid')]
public string $closure;

#[Rule(['string','uuid'])]
public string $closure;
```

### Same

[Docs](https://laravel.com/docs/9.x/validation#rule-same)

```
#[Same('other_field')]
public string $closure;
```

### Size

[Docs](https://laravel.com/docs/9.x/validation#rule-size)

```
#[Size(10)]
public string $closure;
```

### Sometimes

[Docs](https://laravel.com/docs/9.x/validation#validating-when-present)

```
#[Sometimes]
public string $closure;
```

### StartsWith

[Docs](https://laravel.com/docs/9.x/validation#rule-starts-with)

```
#[StartsWith('a')]
public string $closure;

#[StartsWith(['a', 'b'])]
public string $closure;

#[StartsWith('a', 'b')]
public string $closure;
```

### StringType

[Docs](https://laravel.com/docs/9.x/validation#rule-string)

```
#[StringType()]
public string $closure;
```

### TimeZone

[Docs](https://laravel.com/docs/9.x/validation#rule-timezone)

```
#[TimeZone()]
public string $closure;
```

### Unique

[Docs](https://laravel.com/docs/9.x/validation#rule-unique)

```
#[Unique('users')]
public string $closure;

#[Unique(User::class)]
public string $closure;

#[Unique('users', 'email')]
public string $closure;

#[Unique('users', connection: 'tenant')]
public string $closure;

#[Unique('users', withoutTrashed: true)]
public string $closure;

#[Unique('users', ignore: 5)]
public string $closure;
```

### Uppercase

[Docs](https://laravel.com/docs/9.x/validation#rule-uppercase)

```
#[Uppercase]
public string $closure;
```

### Url

[Docs](https://laravel.com/docs/9.x/validation#rule-url)

```
#[Url]
public string $closure;
```

### Ulid

[Docs](https://laravel.com/docs/9.x/validation#rule-ulid)

```
#[Ulid]
public string $closure;
```

### Uuid

[Docs](https://laravel.com/docs/9.x/validation#rule-uuid)

```
#[Uuid]
public string $closure;
```
