โœ…
Laravel Validated DTO
View on GitHub
  • โœ…Validated DTO for Laravel
  • ๐Ÿš€Getting Started
    • Installation
    • Configuration
    • Upgrade Guide
    • Changelog
  • ๐Ÿ˜ŽThe Basics
    • Generating DTOs
    • Defining DTO Properties
    • Defining Validation Rules
    • Creating DTO Instances
    • Accessing DTO Data
    • Defining Default Values
    • Transforming DTO Data
    • Mapping DTO properties
    • Simple DTOs
    • Resource DTOs
    • Wireable DTOs
    • Lazy Validation
    • Generating TypeScript Definitions
  • ๐ŸŽจCustomize
    • Custom Error Messages and Attributes
    • Custom Exceptions
  • ๐ŸงชType Casting
    • Introduction
    • Available Types
    • Create Your Own Type Cast
    • Casting Eloquent Model properties to DTOs
Powered by GitBook
On this page

Was this helpful?

  1. The Basics

Lazy Validation

If you want your DTO to have validation, but not to validate while creating the DTO, you can set your ValidatedDTO to use the Lazy Validation feature.

class LazyDTO extends ValidatedDTO
{
    public bool $lazyValidation = true;
}

When you set the $lazyValidation as true, you can instantiate your DTO without setting any attributes. When setting attributes on your DTO, it won't cast them automatically, the attributes will be cast only when you validate the DTO data by calling the validate() method.

$dto = new LazyDTO();
$dto->name = 'John Doe';
$dto->validate(); // This is where the data is validated and the attributes are cast

If the validation passes, the attributes will be cast in the DTO object.

If the validation fails, it will throw a Illuminate\Validation\ValidationException.

This is a useful feature when using ValidatedDTOs with Livewire.

use Livewire\Wireable as LivewireWireable;
use WendellAdriel\ValidatedDTO\Concerns\Wireable;

class MyDTO extends ValidatedDTO implements LivewireWireable
{
    use Wireable;
    
    public bool $lazyValidation = true;
}
PreviousWireable DTOsNextGenerating TypeScript Definitions

Last updated 6 months ago

Was this helpful?

๐Ÿ˜Ž