Comments (3)
I've found the best approach for me is to keep validation rules within a command (like rules in form requests in Laravel 4.3). To do that you need to use a custom translator and to define a validator class, e.g.:
class CommandTranslator extends BasicCommandTranslator {
public function toValidator($command)
{
return 'Acme\Commander\CommandValidator';
}
}
And your CommandValidator might look like:
class CommandValidator
{
/**
* Validate a command
*
* @param Command $command
* @return bool
* @throws \Acme\Exceptions\ValidationException
*/
public function validate($command)
{
if(method_exists($command, 'rules')) {
$rules = $command->rules();
if(!empty($rules)) {
$validator = \Validator::make((array) $command, $rules);
if($validator->fails()) {
throw new ValidationException($validator->getMessageBag());
}
}
}
return true;
}
}
Now, you do not need to create a validation class for each command, just put:
public function rules()
{
return []; // define your rules here
}
Hope this helps :)
P.S. Don't forget to bind a custom translator. You can drop it to your global.php, e.g:
App::bind('Laracasts\Commander\CommandTranslator', 'Acme\Commander\CommandTranslator');
from commander.
Validation has now been ripped out for Laravel 5.
from commander.
Hi Jeffrey,
shouldn't the command have it's own validation separate from the form validation and the domain/model validation?
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 suer 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 valudated 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 otehr may think of this as duplication but it really isn't as all these layers have seperate concerns and to be fair perform different unrelated processes.
I'd appreciate your thoughts on this if you get a chance :)
from commander.
Related Issues (20)
- Triggering executeDecorators before validateCommand (in ValidationCommandBus) HOT 1
- Laravel 5 HOT 2
- Method injection CommandHandler HOT 5
- Include a demo basic example project in repo HOT 1
- FIX mapInputToCommand accept $input null
- Testing CommandHandlers with PHPSpec?
- PHPSpec a CommandHandler with DispatchableTrait HOT 1
- CommanderTrait method getCommandBus detected as Controller action HOT 1
- Make mapInputToCommand optional in CommanderTrait HOT 7
- Returning created entity HOT 4
- Validation is being fired twice when using ValidationCommandBus
- Could the package allow for custom Mustache templates to be used when generating commands?
- Decorators of the parent command will be executed on the child command HOT 1
- Warning: Ambiguous class resolution HOT 1
- Specify properties types
- Could this be used in laravel 5 HOT 3
- Using CommanderTrait with Console commands
- Javascript Error on Lead deal analysis HOT 1
- Is this project maintained and usable in production? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commander.