PHP code example of overtrue / validation

1. Go to this page and download the library: Download overtrue/validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

overtrue / validation example snippets




use Overtrue\Validation\Translator;
use Overtrue\Validation\Factory as ValidatorFactory;

//初始化工厂对象
$factory = new ValidatorFactory(new Translator);


//验证
$rules = [
    'username' => '    print_r($validator->messages()->all()); // 或者 $validator->errors();
}


$messages = [
    'accepted'             => ':attribute 必须接受。',
    'active_url'           => ':attribute 不是一个有效的网址。',
    'after'                => ':attribute 必须是一个在 :date 之后的日期。',
    'alpha'                => ':attribute 只能由字母组成。',
    'alpha_dash'           => ':attribute 只能由字母、数字和斜杠组成。',
    'alpha_num'            => ':attribute 只能由字母和数字组成。',
    // ...
];

//初始化工厂对象
$factory = new ValidatorFactory(new Translator($messages));


$attributes = [
    'username' => '用户名',
    'password' => '密码',
];

$rules = [
    'username' => 'factory 的时候已经设置了消息,则留空即可

$validator = $factory->make($input, $rules, $messages, $attributes);