This is the documentation for v3 but the latest version is v4 . You can switch versions in the menu on the left/at the top. Check your current version with the following command:
composer show spatie/laravel-data
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>|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
#[Accepted] public bool $closure;
##AcceptedIf
#[AcceptedIf('other_field', 'equals_this')] public bool $closure;
##ActiveUrl
#[ActiveUrl] public string $closure;
##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
#[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
#[Alpha] public string $closure;
##AlphaDash
#[AlphaDash] public string $closure;
##AlphaNumeric
#[AlphaNumeric] public string $closure;
##ArrayType
#[ArrayType] public array $closure; #[ArrayType(['valid_key', 'other_valid_key'])] public array $closure; #[ArrayType('valid_key', 'other_valid_key')] public array $closure;
##Bail
#[Bail] public string $closure;
##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
#[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
#[Between(3.14, 42)] public int $closure;
##BooleanType
#[BooleanType] public bool $closure;
##Confirmed
#[Confirmed] public string $closure;
##CurrentPassword
#[CurrentPassword] public string $closure; #[CurrentPassword('api')] public string $closure;
##Date
#[Date] public Carbon $closure;
##DateEquals
#[DateEquals('tomorrow')] public Carbon $closure; #[DateEquals(Carbon::yesterday())] public Carbon $closure;
##DateFormat
#[DateFormat('d-m-Y')] public Carbon $closure;
##Declined
#[Declined] public bool $closure;
##DeclinedIf
#[DeclinedIf('other_field', 'equals_this')] public bool $closure;
##Different
#[Different('other_field')] public string $closure;
##Digits
#[Digits(10)] public int $closure;
##DigitsBetween
#[DigitsBetween(2, 10)] public int $closure;
##Dimensions
#[Dimensions(ratio: 1.5)] public UploadedFile $closure; #[Dimensions(maxWidth: 100, maxHeight: 100)] public UploadedFile $closure;
##Distinct
#[Distinct] public string $closure; #[Distinct(Distinct::Strict)] public string $closure; #[Distinct(Distinct::IgnoreCase)] public string $closure;
##DoesntEndWith
#[DoesntEndWith('a')] public string $closure; #[DoesntEndWith(['a', 'b'])] public string $closure; #[DoesntEndWith('a', 'b')] public string $closure;
##DoesntStartWith
#[DoesntStartWith('a')] public string $closure; #[DoesntStartWith(['a', 'b'])] public string $closure; #[DoesntStartWith('a', 'b')] public string $closure;
#[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
#[EndsWith('a')] public string $closure; #[EndsWith(['a', 'b'])] public string $closure; #[EndsWith('a', 'b')] public string $closure;
##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
#[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
#[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
#[ExcludeWith('other_field')] public string $closure;
##ExcludeWithout
At the moment the data is not yet excluded due to technical reasons, v4 should fix this
#[ExcludeWithout('other_field')] public string $closure;
##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
#[File] public UploadedFile $closure;
##Filled
#[Filled] public string $closure;
##GreaterThan
#[GreaterThan('other_field')] public int $closure;
##GreaterThanOrEqualTo
#[GreaterThanOrEqualTo('other_field')] public int $closure;
##Image
#[Image] public UploadedFile $closure;
##In
#[In([1, 2, 3, 'a', 'b'])] public mixed $closure; #[In(1, 2, 3, 'a', 'b')] public mixed $closure;
##InArray
#[InArray('other_field')] public string $closure;
##IntegerType
#[IntegerType] public int $closure;
##IP
#[IP] public string $closure;
##IPv4
#[IPv4] public string $closure;
##IPv6
#[IPv6] public string $closure;
##Json
#[Json] public string $closure;
##LessThan
#[LessThan('other_field')] public int $closure;
##LessThanOrEqualTo
#[LessThanOrEqualTo('other_field')] public int $closure;
##Lowercase
#[Lowercase] public string $closure;
##MacAddress
#[MacAddress] public string $closure;
##Max
#[Max(20)] public int $closure;
##MaxDigits
#[MaxDigits(10)] public int $closure;
##MimeTypes
#[MimeTypes('video/quicktime')] public UploadedFile $closure; #[MimeTypes(['video/quicktime', 'video/avi'])] public UploadedFile $closure; #[MimeTypes('video/quicktime', 'video/avi')] public UploadedFile $closure;
##Mimes
#[Mimes('jpg')] public UploadedFile $closure; #[Mimes(['jpg', 'png'])] public UploadedFile $closure; #[Mimes('jpg', 'png')] public UploadedFile $closure;
##Min
#[Min(20)] public int $closure;
##MinDigits
#[MinDigits(2)] public int $closure;
##MultipleOf
#[MultipleOf(3)] public int $closure;
##NotIn
#[NotIn([1, 2, 3, 'a', 'b'])] public mixed $closure; #[NotIn(1, 2, 3, 'a', 'b')] public mixed $closure;
##NotRegex
#[NotRegex('/^.+$/i')] public string $closure;
##Nullable
#[Nullable] public ?string $closure;
##Numeric
#[Numeric] public ?string $closure;
##Password
#[Password(min: 12, letters: true, mixedCase: true, numbers: false, symbols: false, uncompromised: true, uncompromisedThreshold: 0)] public string $closure;
##Present
#[Present] public string $closure;
##Prohibited
#[Prohibited] public ?string $closure;
##ProhibitedIf
#[ProhibitedIf('other_field', 'has_value')] public ?string $closure; #[ProhibitedIf('other_field', ['has_value', 'or_this_value'])] public ?string $closure;
##ProhibitedUnless
#[ProhibitedUnless('other_field', 'has_value')] public ?string $closure; #[ProhibitedUnless('other_field', ['has_value', 'or_this_value'])] public ?string $closure;
##Prohibits
#[Prohibits('other_field')] public ?string $closure; #[Prohibits(['other_field', 'another_field'])] public ?string $closure; #[Prohibits('other_field', 'another_field')] public ?string $closure;
##Regex
#[Regex('/^.+$/i')] public string $closure;
##Required
#[Required] public string $closure;
##RequiredIf
#[RequiredIf('other_field', 'value')] public ?string $closure; #[RequiredIf('other_field', ['value', 'another_value'])] public ?string $closure;
##RequiredUnless
#[RequiredUnless('other_field', 'value')] public ?string $closure; #[RequiredUnless('other_field', ['value', 'another_value'])] public ?string $closure;
##RequiredWith
#[RequiredWith('other_field')] public ?string $closure; #[RequiredWith(['other_field', 'another_field'])] public ?string $closure; #[RequiredWith('other_field', 'another_field')] public ?string $closure;
##RequiredWithAll
#[RequiredWithAll('other_field')] public ?string $closure; #[RequiredWithAll(['other_field', 'another_field'])] public ?string $closure; #[RequiredWithAll('other_field', 'another_field')] public ?string $closure;
##RequiredWithout
#[RequiredWithout('other_field')] public ?string $closure; #[RequiredWithout(['other_field', 'another_field'])] public ?string $closure; #[RequiredWithout('other_field', 'another_field')] public ?string $closure;
##RequiredWithoutAll
#[RequiredWithoutAll('other_field')] public ?string $closure; #[RequiredWithoutAll(['other_field', 'another_field'])] public ?string $closure; #[RequiredWithoutAll('other_field', 'another_field')] public ?string $closure;
##RequiredArrayKeys
#[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
#[Same('other_field')] public string $closure;
##Size
#[Size(10)] public string $closure;
##Sometimes
#[Sometimes] public string $closure;
##StartsWith
#[StartsWith('a')] public string $closure; #[StartsWith(['a', 'b'])] public string $closure; #[StartsWith('a', 'b')] public string $closure;
##StringType
#[StringType()] public string $closure;
##TimeZone
#[TimeZone()] public string $closure;
##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
#[Uppercase] public string $closure;
##Url
#[Url] public string $closure;
##Ulid
#[Ulid] public string $closure;
##Uuid
#[Uuid] public string $closure;