Home Backend Development PHP Tutorial Implementation of custom validator in thinkPHP5 framework

Implementation of custom validator in thinkPHP5 framework

Jun 11, 2018 am 10:04 AM
thinkphp5

This article mainly introduces the implementation method of the thinkPHP5 framework custom validator, and analyzes the specific definition and usage of thinkPHP custom validator in the form of examples. Friends in need can refer to the following

The examples of this article are described Developed the thinkPHP5 framework custom validator implementation method. Share it with everyone for your reference, the details are as follows:

The ordinary validator manual is very detailed, let’s explain how to customize a validator

First we create the validata file in the module directory Folder

Then create a class in it and name it IdMustInt.php

The code is as follows: Note that my module is called api, so the namespace is as follows

Protect attributes $rule is an official rule and cannot be changed. In fact, the verification rules require are all encapsulated function names, so we also create a method. The method name is filled in after the verification rule.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

namespace app\api\validate;

use think\Validate;

class IdMustInt extends Validate

{

  protected $rule = [

    'id' => 'require|IsInt'

  ];

  protected function IsInt($value,$rule,$data,$field){

  //参数依次为验证数据,验证规则,全部数据(数组),字段名

  //这里我们要判断的验证的数据要求必须为正整型

    if(is_numeric($value) && is_int($value+0) && ($value+0) > 0){

      return true;

    }else{

  //如果不符合我们的条件,返回错误信息,在控制器中可以用getError()方法输出

      return $field.'不是整型';

    }

  }

}

Copy after login

Next, let’s look at our controller The corresponding operations

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public function getBanner($id)

{

    //需要验证的数据

    $data = [

      'id' => $id,

    ];

  //实例化验证器

    $validate = new IdMustInt();

  //如果验证数据较多,条件也较多,需要批量返回所有错误信息的话,可以在check()前加上$validata->batch()

    $result = $validate->check($data);

    if($result){

      //业务逻辑

    }else{

      dump($validate->getError());

    }

}

Copy after login

##The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of functions and usage of widgets in thinkPHP5 framework

##About the implementation of data addition and display in thinkphp framework Function method

The above is the detailed content of Implementation of custom validator in thinkPHP5 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I get an error when deploying thinkphp5 in Pagoda? What should I do if I get an error when deploying thinkphp5 in Pagoda? Dec 19, 2022 am 11:04 AM

What should I do if I get an error when deploying thinkphp5 in Pagoda?

What should I do if thinkphp5 post cannot get the value? What should I do if thinkphp5 post cannot get the value? Dec 06, 2022 am 09:29 AM

What should I do if thinkphp5 post cannot get the value?

How to get the requested URL in thinkphp5 How to get the requested URL in thinkphp5 Dec 20, 2022 am 09:48 AM

How to get the requested URL in thinkphp5

What should I do if thinkphp5 url rewriting fails? What should I do if thinkphp5 url rewriting fails? Dec 12, 2022 am 09:31 AM

What should I do if thinkphp5 url rewriting fails?

How to remove thinkphp5 title bar icon How to remove thinkphp5 title bar icon Dec 20, 2022 am 09:24 AM

How to remove thinkphp5 title bar icon

What should I do if thinkphp5 prompts that the controller does not exist? What should I do if thinkphp5 prompts that the controller does not exist? Dec 06, 2022 am 10:43 AM

What should I do if thinkphp5 prompts that the controller does not exist?

How to query yesterday's data in ThinkPHP5 How to query yesterday's data in ThinkPHP5 Dec 05, 2022 am 09:20 AM

How to query yesterday's data in ThinkPHP5

How to set error prompts in thinkphp5 How to set error prompts in thinkphp5 Dec 07, 2022 am 10:31 AM

How to set error prompts in thinkphp5

See all articles