PHP code example of death_satan / thinkphp-form-request

1. Go to this page and download the library: Download death_satan/thinkphp-form-request 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/ */

    

death_satan / thinkphp-form-request example snippets




namespace app\FormRequest;

use SaTan\Think\Request\FormRequest;
use think\Validate;

class IndexRequest extends FormRequest
{
    /*
     *初始化
     */
    protected function initialization ():void
    {

    }

    /**
     * @var Validate|string 验证器
     */
    protected $validate;

    /**
     *
     * @var bool $batch 批量验证
     */
    protected $batch = false;

    /**
     * 验证权限
     * @return bool
     */
    protected  function check ():bool
    {
        return true;
    }

    /**
     * 验证规则
     * @return array
     */
    protected function rules():array
    {
        return [
        ];
    }

    /**
     * 错误消息
     * @return array
     */
    protected function messages():array
    {
        return [

        ];
    }

    /**
     * 验证前置操作
     * @param Validate $validate
     */
    protected function withValidate(Validate $validate):void
    {

    }

}
 

    /**
     * @var Validate|string 验证器
     */
    protected $validate = \app\validate\Test::class;

class Index extends BaseController
{
    public function index(TestRequest $request)
    {
        //验证成功后数据处理...
        dd($request->all());
    }
}
 

    /**
     *
     * @var bool $batch 批量验证
     */
    protected $batch = false;

    /**
     * 验证权限
     * @return bool
     */
    protected  function check ():bool
    {
        return true;
    }

    /**
     * 验证规则
     * @return array
     */
    protected function rules():array
    {
        return [

        ];
    }

    /**
     * 错误消息
     * @return array
     */
    protected function messages():array
    {
        return [

        ];
    }


 
    /**
     * 验证前置操作
     * @param Validate $validate
     */
    protected function withValidate(Validate $validate):void
    {
            //验证数据
            $check = $validate->check($this->param());

            if ($check!==true)
            {
                //接管自定义的验证
//                $this->throwCheckError((array)$validate->getError());
            }
    }
shell
# 执行下面的指令可以生成index应用的Blog表单验证器类库文件
php think make:request index@IndexRequest
# app/index/FormRequest/IndexRequest

#如果是单应用模式,则无需传入应用名
php think make:request IndexRequest
# app/FormRequest/IndexRequest

#如果需要生成多级表单验证器,可以使用
php think make:controller index@test/IndexRequest
# app/index/test/Blog/IndexRequest

shell
php think make:validate Test
#会在 app/validate/目录下生成一个 Test验证器
shell
php think make:request TestRequest
#会在 app/FormRequest目录下生成一个TestRequest表单验证器类库