Skip to content

[12.x] Add HasValidation Trait #55986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

WendellAdriel
Copy link
Contributor

@WendellAdriel WendellAdriel commented Jun 11, 2025

Changes

This PR adds a new trait: HasValidation. This was heavily inspired (and most of its implementation is based also) in my Validated DTO package. The trait is an abstraction around the Validator facade, making it easier to create any types of classes with a built-in validation system.

Why to have this Trait

The Validator facade already helps a lot on creating validations for any classes we want, this is just a small improvement on DX that will abstract the "boilerplate" code.

With this, any classes (even Models) can define their validations by simply using the Trait and defining a rules method. But it also gives control for setting custom messages with the messages method, setting custom attributes with the attributes method and it also supports the after validation of the Validator.

Example

class DummyUser
{
    use HasValidation;

    public string $email;

    public string $name;

    protected function rules(): array
    {
        return [
            'email' => ['required', 'email'],
            'name' => ['required', 'string', 'min:3'],
        ];
    }
}

$user = new DummyUser();
$user->email = 'test@laravel.com';
$user->name = 'a';
$user->validate(); // Throw ValidationException

$user->name = 'Taylor';
$user->validate(); // No exception is thrown

More examples can be found in the Test file in this PR.

@shaedrich
Copy link
Contributor

Nice! A follow-up would be to define auto-triggers for validation (e.g. validate on save)

@WendellAdriel
Copy link
Contributor Author

Nice! A follow-up would be to define auto-triggers for validation (e.g. validate on save)

If this one gets approved, that can be a nice follow-up indeed!!! 💪

@cosmastech
Copy link
Contributor

One step closer to making Request objects classes with typed properties 💪

@devajmeireles
Copy link
Contributor

While I understood the purpose of this, now that I understand a little of Taylor's vision, I would say that this is a type of feature that you should create into your project or offer as a package.

@shaedrich
Copy link
Contributor

I fully expect Taylor to close this but that does not mean that I wouldn't love having this feature in Laravel

@WendellAdriel
Copy link
Contributor Author

Yeah @devajmeireles @shaedrich
We never know if we don't try 😅

I don't mind having PRs closed, I already contributed some that were merged and some others that were closed.
At the end it's always good to practice working on the codebase and learning something new along the way! 💪

@devajmeireles
Copy link
Contributor

Yeah @devajmeireles @shaedrich We never know if we don't try 😅

I don't mind having PRs closed, I already contributed some that were merged and some others that were closed. At the end it's always good to practice working on the codebase and learning something new along the way! 💪

You're absolutely right. I would also say that it brings us benefits, I can see the benefits of it, but, as I said, I see it more as a question of project design.

@WendellAdriel
Copy link
Contributor Author

Yeah @devajmeireles @shaedrich We never know if we don't try 😅
I don't mind having PRs closed, I already contributed some that were merged and some others that were closed. At the end it's always good to practice working on the codebase and learning something new along the way! 💪

You're absolutely right. I would also say that it brings us benefits, I can see the benefits of it, but, as I said, I see it more as a question of project design.

Exactly! I would like to have this built-in the framework because I think it can help, but let's see what Taylor and the team will decide.

@taylorotwell
Copy link
Member

I personally feel this is simple enough to implement in user-land I would prefer to leave it as a package. 👍

@WendellAdriel WendellAdriel deleted the feature/has-validation-trait branch June 11, 2025 22:24
@WendellAdriel
Copy link
Contributor Author

I personally feel this is simple enough to implement in user-land I would prefer to leave it as a package. 👍

Got it,thanks! 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants