PHP code example of taoser / webman-validate

1. Go to this page and download the library: Download taoser/webman-validate 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/ */

    

taoser / webman-validate example snippets


namespace app\validate;

use taoser\Validate;

class User extends Validate
{
    protected $rule = [
        'name'  =>  '


namespace app\index\validate;

use taoser\Validate;

class User extends Validate
{
	// 定义规则
    protected $rule =   [
        'name'  => 'uire' => '名称必须',
        'name.max'     => '名称最多不能超过25个字符',
        'age.number'   => '年龄必须是数字',
        'age.between'  => '年龄只能在1-120之间',
        'email'        => '邮箱格式错误',    
    ];
	
	//定义场景
	protected $scene = [
        'edit'  =>  ['name','age'],
    ];
    
}


namespace app\controller;

use app\validate\User;
use taoser\exception\ValidateException;

class Index
{
    public function index()
    {
        try {
            validate(User::class)->check([
                'name'  => 'thinkphp',
                'email' => '[email protected]',
            ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            dump($e->getError());
        }
    }
}


	//场景校验方法
	$data = [
		'name'  => 'thinkphp',
		'age'   => 10,
		'email' => '[email protected]',
	];

	try {
		validate(app\validate\User::class)
			->scene('edit')
			->check($data);
	} catch (ValidateException $e) {
		// 验证失败 输出错误信息
		dump($e->getError());
	}
	
	// 门面方法验证
	
	$validate = \taoser\facade\Validate::rule('age', 'number|between:1,120')
	->rule([
		'name'  => '