Support the ongoing development of Laravel.io →
Validation Packages Architecture
Last updated 2 years ago.
0

@mnshankar

Thanks for sharing. I think this is a good solution for a quick prototype or a small CRUD application. However, I don't agree with having the form validation within the command validator.

At the moment using L4 I use:

a) form validator which returns user friendly messages and who's sole interest is the user input. I call this from the controller before executing the commands because the transport ios directly related to the view.

b) I have a command validator whos purpose is to validate the data passed to the command. In my opinion this is generally different from the form as a form can have recaptcha and other user verification methods. The command is not interested on what data was inputted or displayed to the user but only to handle the command (not the form or the database).

c) The model itself will have it's own specification services. Different from actual input validation as this validated objects with their types and properties normally using typecasting.

d) Most infrastructure services will have their own rules and validators such as the ORM, API services etc etc...

I understand others may think of this as duplication but it really isn't as all these layers have separate concerns and to be fair perform different unrelated processes.

In L5 we have some more helpers and this can be simplified as you can inject the form validator in the controller method etc..

Last updated 2 years ago.
0

The other option to validate the data before passing the command to the CommandHandler can be accomplished by decorating the command bus. You could validate or sanitize the data by creating a JobSanitizer class as follows :

<?php namespace Acme\Jobs;

use Laracasts\Commander\CommandBus;
use Laracasts\Validation\FormValidator;
class JobSanitizer extends FormValidator implements CommandBus{

    protected $rules = [
        'title' => 'required'
    ];

    public function execute($command){
        //sanitize the job data
        $this->validate($command);
    }
}

and now reference this class, when you execute the command in your controller.

$this->execute(PostJobListingCommand::class, null, [
    'Acme\Jobs\JobSanitizer'
]);

This would make sure that the JobSanitizer's execute method is called before the execute method of the CommandBus is called. This gives you the flexibility to decorate the CommandBus how much ever you want.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mnshankar mnshankar Joined 13 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.