This is the documentation for v2 but the latest version is v3 . 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
A validation attribute is a class that extends ValidationRule
and returns an array of validation rules when the getRules
method is called:
#[Attribute(Attribute::TARGET_PROPERTY)]
class CustomRule extends ValidationRule
{
public function getRules(): array
{
return [new CustomRule()];
}
}
##Available validation attributes
##Accepted
#[Accepted]
public bool $value;
##AcceptedIf
#[AcceptedIf('other_field', 'equals_this')]
public bool $value;
##ActiveUrl
#[ActiveUrl]
public string $value;
##After
#[After('other_field')]
public Carbon $value;
#[After('tomorrow')]
public Carbon $value;
#[After(Carbon::yesterday())]
public Carbon $value;
##AfterOrEqual
#[AfterOrEqual('other_field')]
public Carbon $value;
#[AfterOrEqual('tomorrow')]
public Carbon $value;
#[AfterOrEqual(Carbon::yesterday())]
public Carbon $value;
##Alpha
#[Alpha]
public string $value;
##AlphaDash
#[AlphaDash]
public string $value;
##AlphaNumeric
#[AlphaNumeric]
public string $value;
##ArrayType
#[ArrayType]
public array $value;
#[ArrayType(['valid_key', 'other_valid_key'])]
public array $value;
#[ArrayType('valid_key', 'other_valid_key')]
public array $value;
##Bail
#[Bail]
public string $value;
##Before
#[Before('other_field')]
public Carbon $value;
#[Before('tomorrow')]
public Carbon $value;
#[Before(Carbon::yesterday())]
public Carbon $value;
##BeforeOrEqual
#[BeforeOrEqual('other_field')]
public Carbon $value;
#[BeforeOrEqual('tomorrow')]
public Carbon $value;
#[BeforeOrEqual(Carbon::yesterday())]
public Carbon $value;
##Between
#[Between(3.14, 42)]
public int $value;
##BooleanType
#[BooleanType]
public bool $value;
##Confirmed
#[Confirmed]
public string $value;
##CurrentPassword
#[CurrentPassword]
public string $value;
#[CurrentPassword('api')]
public string $value;
##Date
#[Date]
public Carbon $value;
##DateEquals
#[DateEquals('tomorrow')]
public Carbon $value;
#[DateEquals(Carbon::yesterday())]
public Carbon $value;
##DateFormat
#[DateFormat('d-m-Y')]
public Carbon $value;
##Different
#[Different('other_field')]
public string $value;
##Digits
#[Digits(10)]
public int $value;
##DigitsBetween
#[DigitsBetween(2, 10)]
public int $value;
##Dimensions
#[Dimensions(ratio: 1.5)]
public UploadedFile $value;
#[Dimensions(maxWidth: 100, maxHeight: 100)]
public UploadedFile $value;
##Distinct
#[Distinct]
public string $value;
#[Distinct(Distinct::Strict)]
public string $value;
#[Distinct(Distinct::IgnoreCase)]
public string $value;
#[Email]
public string $value;
#[Email(Email::RfcValidation)]
public string $value;
#[Email([Distinct::RfcValidation, Distinct::DnsCheckValidation])]
public string $value;
#[Email(Distinct::RfcValidation, Distinct::DnsCheckValidation)]
public string $value;
##EndsWith
#[EndsWith('a')]
public string $value;
#[EndsWith(['a', 'b'])]
public string $value;
#[EndsWith('a', 'b')]
public string $value;
##Enum
#[Enum(ChannelType::class)]
public string $value;
##ExcludeIf
#[ExcludeIf('other_field', 'has_value')]
public string $value;
##ExcludeUnless
#[ExcludeUnless('other_field', 'has_value')]
public string $value;
##ExcludeWithout
#[ExcludeWithout('other_field')]
public string $value;
##Exists
#[Exists('users')]
public string $value;
#[Exists(User::class)]
public string $value;
#[Exists('users', 'email')]
public string $value;
#[Exists('users', 'email', connection: 'tenant')]
public string $value;
#[Exists('users', 'email', withoutTrashed: true)]
public string $value;
##File
#[File]
public UploadedFile $value;
##Filled
#[Filled]
public string $value;
##GreaterThan
#[GreaterThan('other_field')]
public int $value;
##GreaterThanOrEqualTo
#[GreaterThanOrEqualTo('other_field')]
public int $value;
##Image
#[Image]
public UploadedFile $value;
##In
#[In([1, 2, 3, 'a', 'b'])]
public mixed $value;
#[In(1, 2, 3, 'a', 'b')]
public mixed $value;
##InArray
#[InArray('other_field')]
public string $value;
##IntegerType
#[IntegerType]
public int $value;
##IP
#[IP]
public string $value;
##IPv4
#[IPv4]
public string $value;
##IPv6
#[IPv6]
public string $value;
##Json
#[Json]
public string $value;
##LessThan
#[LessThan('other_field')]
public int $value;
##LessThanOrEqualTo
#[LessThanOrEqualTo('other_field')]
public int $value;
##Max
#[Max(20)]
public int $value;
##MimeTypes
#[MimeTypes('video/quicktime')]
public UploadedFile $value;
#[MimeTypes(['video/quicktime', 'video/avi'])]
public UploadedFile $value;
#[MimeTypes('video/quicktime', 'video/avi')]
public UploadedFile $value;
##Mimes
#[Mimes('jpg')]
public UploadedFile $value;
#[Mimes(['jpg', 'png'])]
public UploadedFile $value;
#[Mimes('jpg', 'png')]
public UploadedFile $value;
##Min
#[Min(20)]
public int $value;
##MultipleOf
#[MultipleOf(3)]
public int $value;
##NotIn
#[NotIn([1, 2, 3, 'a', 'b'])]
public mixed $value;
#[NotIn(1, 2, 3, 'a', 'b')]
public mixed $value;
##NotRegex
#[NotRegex('/^.+$/i')]
public string $value;
##Nullable
#[Nullable]
public ?string $value;
##Numeric
#[Numeric]
public ?string $value;
##Password
#[Password(min: 12, letters: true, mixedCase: true, numbers: false, symbols: false, uncompromised: true, uncompromisedThreshold: 0)]
public string $value;
##Present
#[Present]
public string $value;
##Prohibited
#[Prohibited]
public ?string $value;
##ProhibitedIf
#[ProhibitedIf('other_field', 'has_value')]
public ?string $value;
#[ProhibitedIf('other_field', ['has_value', 'or_this_value'])]
public ?string $value;
##ProhibitedUnless
#[ProhibitedUnless('other_field', 'has_value')]
public ?string $value;
#[ProhibitedUnless('other_field', ['has_value', 'or_this_value'])]
public ?string $value;
##Prohibits
#[Prohibits('other_field')]
public ?string $value;
#[Prohibits(['other_field', 'another_field'])]
public ?string $value;
#[Prohibits('other_field', 'another_field')]
public ?string $value;
##Regex
#[Regex('/^.+$/i')]
public string $value;
##Required
#[Required]
public string $value;
##RequiredIf
#[RequiredIf('other_field', 'value')]
public ?string $value;
#[RequiredIf('other_field', ['value', 'another_value'])]
public ?string $value;
##RequiredUnless
#[RequiredUnless('other_field', 'value')]
public ?string $value;
#[RequiredUnless('other_field', ['value', 'another_value'])]
public ?string $value;
##RequiredWith
#[RequiredWith('other_field')]
public ?string $value;
#[RequiredWith(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWith('other_field', 'another_field')]
public ?string $value;
##RequiredWithAll
#[RequiredWithAll('other_field')]
public ?string $value;
#[RequiredWithAll(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithAll('other_field', 'another_field')]
public ?string $value;
##RequiredWithout
#[RequiredWithout('other_field')]
public ?string $value;
#[RequiredWithout(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithout('other_field', 'another_field')]
public ?string $value;
##RequiredWithoutAll
#[RequiredWithoutAll('other_field')]
public ?string $value;
#[RequiredWithoutAll(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithoutAll('other_field', 'another_field')]
public ?string $value;
##Rule
#[Rule('string|uuid')]
public string $value;
#[Rule(['string','uuid'])]
public string $value;
##Same
#[Same('other_field')]
public string $value;
##Size
#[Size(10)]
public string $value;
##Sometimes
#[Sometimes]
public string $value;
##StartsWith
#[StartsWith('a')]
public string $value;
#[StartsWith(['a', 'b'])]
public string $value;
#[StartsWith('a', 'b')]
public string $value;
##StringType
#[StringType()]
public string $value;
##TimeZone
#[TimeZone()]
public string $value;
##Unique
#[Unique('users')]
public string $value;
#[Unique(User::class)]
public string $value;
#[Unique('users', 'email')]
public string $value;
#[Unique('users', connection: 'tenant')]
public string $value;
#[Unique('users', withoutTrashed: true)]
public string $value;
#[Unique('users', ignore: 5)]
public string $value;
##Url
#[Url]
public string $value;
##Uuid
#[Uuid]
public string $value;