Git Product home page Git Product logo

ray.validatemodule's Introduction

Ray.ValidateModule

Scrutinizer Code Quality Code Coverage Build Status Build Status

Installation

Composer install

$ composer require ray/validate-module

Module install

use Ray\Di\AbstractModule;
use Ray\AuraSqlModule\AuraSqlModule;
use Ray\AuraSqlModule\Annotation\AuraSqlConfig;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new ValidateModule);
    }
}

Usage

Annotate target method with @Valid annotation.

use Ray\Validation\Annotation\Valid;

class User
{
    /**
     * @Valid
     */
    public function createUser($name)
    {
        // ...
    }

Provide onValidate annotated method in same class for validation. Method can be named freely.

use Ray\Validation\Annotation\OnValidate;
// ...

    /**
     * @OnValidate
     */
    public function onValidate($name)
    {
        $validation = new Validation;
        if (! is_string($name)) {
            $validation->addError('name', 'name should be string');
        }

        return $validation;
    }

Validate all parameters in validation method. addError($name, $message) with invalid parameter name and message in the case of validation failed.

Ray\Validation\Exception\InvalidArgumentException thrown on validation failed, But if @OnFailure annoted method exists, The result of @OnFailure method returns instead of original.

use Ray\Validation\Annotation\OnFailure;
// ...

    /**
     * @OnFailure
     */
    public function onFailure(FailureInterface $failure)
    {

        // original parameters
        list($this->defaultName) = $failure->getInvocation()->getArguments();

        // errors
        foreach ($failure->getMessages() as $name => $messages) {
            foreach ($messages as $message) {
                echo "Input '{$name}': {$message}" . PHP_EOL;
            }
        }
    }

If you need multiple validation in one class, You can name validation at annotation property as follows.

use Ray\Validation\Annotation\Valid;
use Ray\Validation\Annotation\OnValidate;
use Ray\Validation\Annotation\OnInvalid;
// ...

    /**
     * @Valid("foo")
     */
    public function fooAction($name, $address, $zip)
    {
    
    /**
     * @OnValidate("foo")
     */
    public function onValidateFoo($name, $address, $zip)
    {

    // ...
    /**
     * @OnInvalid("foo")
     */
    public function onInvalidFoo(FailureInterface $failure)
    {
    // ...

Demo

$ php docs/demo/run.php
// It works!

ray.validatemodule's People

Contributors

kenjis avatar koriym avatar kuma-guy avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kenjis koriym hum2

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.