PHP code example of superadminx / think_validate

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

    

superadminx / think_validate example snippets


namespace app\validate;

use superadminx\think_validate\Validate;

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

}


namespace app\controller;

use app\validate\UserValidate;
use superadminx\think_validate\exception\ValidateException;

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