Git Product home page Git Product logo

validation's Introduction

Hyperf Validation

About

hyperf/validation 组件衍生于 Laravel Validation 组件的,我们对它进行了一些改造,大部分功能保持了相同。在这里感谢一下 Laravel 开发组,实现了如此强大好用的 Validation 组件。

Installation

composer require hyperf/validation

Config

Publish config file

# 发布国际化配置,已经发布过国际化配置可以省略
php bin/hyperf.php vendor:publish hyperf/translation

php bin/hyperf.php vendor:publish hyperf/validation

Configuration path

your/config/path/autoload/translation.php

Configuration

<?php
return [
    'locale' => 'zh_CN',
    'fallback_locale' => 'en',
    'path' => BASE_PATH . '/storage/languages',
];

Exception handler

<?php
return [
    'handler' => [
        'http' => [
            \Hyperf\Validation\ValidationExceptionHandler::class,
        ],
    ],
];

Validation middleware

<?php
return [
    'http' => [
        \Hyperf\Validation\Middleware\ValidationMiddleware::class,
    ],
];

Usage

Generate form request

Command:

php bin/hyperf.php gen:request FooRequest

Usage:

class IndexController
{
    public function foo(FooRequest $request)
    {
        $request->input('foo');
    }
    
    public function bar(RequestInterface $request){
        $factory = $this->container->get(\Hyperf\Validation\Contracts\Validation\Factory::class);

        $factory->extend('foo', function ($attribute, $value, $parameters, $validator) {
            return $value == 'foo';
        });

        $factory->replacer('foo', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':foo', $attribute, $message);
        });

        $validator = $factory->make(
            $request->all(),
            [
                'name' => 'required|foo',
            ],
            [
                'name.foo' => ':foo is not foo',
            ]
        );

        if (!$validator->passes()) {
             $validator->errors();
        }
    }
}

validation's People

Contributors

chunhei2008 avatar huangzhhui avatar limingxinleo avatar sy-records avatar yl avatar

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.