Coder Social home page Coder Social logo

Validation Decorator about commander HOT 3 CLOSED

laracasts avatar laracasts commented on August 31, 2024
Validation Decorator

from commander.

Comments (3)

salebab avatar salebab commented on August 31, 2024

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.

JeffreyWay avatar JeffreyWay commented on August 31, 2024

Validation has now been ripped out for Laravel 5.

from commander.

keithmifsud avatar keithmifsud commented on August 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.