Home >Backend Development >PHP Tutorial >How to use thinkphp5.0 verification class

How to use thinkphp5.0 verification class

不言
不言Original
2018-05-07 10:00:201292browse

This article mainly introduces how to use the thinkphp5.0 verification class. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

I will explain it to you through an example. If you pass the thinkphp5.0 verification class method.

Customize the validation class and need to inherit the Validate class

For example, create a new validate folder in the home module, and then create a new Test.php validation class with the following content:

<?php
namespace app\home\validate;
use think\Validate;
class Test extends Validate
{
  protected $rule = [
    &#39;name&#39; => &#39;require|regex:/.{6}/&#39;,
    &#39;age&#39; => &#39;number|between:1,120&#39;,
    &#39;email&#39; => &#39;email&#39;
  ];
  protected $message = [
    &#39;name.require&#39; => &#39;name不能少&#39;,
    &#39;name.regex&#39; => &#39;name不能少于6个字符&#39;,
    &#39;age.number&#39; => &#39;age必须是数字&#39;,
    &#39;age.between&#39; => &#39;age必须在1到120之间&#39;,
    &#39;email.email&#39; => &#39;email格式不对&#39;,
  ];
  protected $scene = [
    &#39;name_email&#39; => [&#39;name&#39;,&#39;email&#39;],
  ];
}
?>

Use

<?php
namespace app\home\controller;
use think\Loader;
use think\Controller;
class Index extends Controller
{
  public function test(){
    $date = [
      &#39;name&#39;=>&#39;qw2e&#39;,
      &#39;email&#39;=>&#39;12313&#39;
    ];
    //$validate = Loader::validate(&#39;Test&#39;);//使用加载类Loader
    $validate = validate(&#39;Test&#39;);//使用助手函数
    $result = $validate->scene(&#39;name_email&#39;)->check($date);
    if(!$result){
      dump($validate->getError());
    }
  }
}

in the Index controller test method. Related recommendations:

thinkPHP5.0 framework application request life cycle analysis

thinkPHP5.0 framework independent configuration and dynamic configuration methods

The above is the detailed content of How to use thinkphp5.0 verification class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn