Home PHP Framework ThinkPHP How to use ThinkPHP6 to implement form validation

How to use ThinkPHP6 to implement form validation

Jun 20, 2023 pm 12:01 PM
thinkphp accomplish form validation

With the development of the Internet, form validation has become an important part of Web development. Without an effective form validation mechanism, the data entered by the user will not be accepted by the system due to format or logic errors, which will have a great impact on the overall user experience and security of the system. ThinkPHP, one of the commonly used frameworks in PHP development, also provides a very convenient and customizable form validation mechanism. This article will introduce how to use ThinkPHP6 to implement form validation.

1. Create a new controller

First, create a controller file in ThinkPHP6. For example, we can create a UserController.php file. In this controller, we will implement a function to update the user's general information and use form validation.

2. Write the data model

Next, we need to create a new User.php file in the Model file to store the model corresponding to the user's data table. In this file, we can define the corresponding field types and data verification rules, as shown below:

namespace appmodel;

use thinkModel;

class User extends Model
{
    // 定义模型对应数据表
    protected $table = 'user';

    // 定义数据表字段对应的验证规则
    protected $rule = [
        'username' => 'require|length:6,20|unique:user',
        'password' => 'require|alphaDash|confirm',
        'email'    => 'require|email|unique:user',
        'mobile'   => 'mobile|unique:user'
    ];

    // 定义验证错误信息
    protected $message = [
        'username.require' => '用户名不能为空',
        'username.length'  => '用户名长度为6-20个字符',
        'username.unique'  => '该用户名已存在',
        'password.require' => '密码不能为空',
        'password.alphaDash'  => '密码只能是字母、数字、下划线和破折号',
        'password.confirm' => '两次输入密码不一致',
        'email.require'    => '邮箱不能为空',
        'email.email'      => '邮箱格式不正确',
        'email.unique'     => '该邮箱已存在',
        'mobile.mobile'    => '手机号格式不正确',
        'mobile.unique'    => '该手机号已存在'
    ];
}
Copy after login

In this model, we first define the table name and define the fields corresponding to the data table. Validation rules and validation error messages. These rules and information will be used in the controller to verify the validity of the data.

3. Write the controller

Next, receive the data in the controller and call the validate() function to verify the data. After successful verification, save the data to the database.

namespace appcontroller;

use appmodelUser;
use thinkacadeRequest;

class UserController extends Base
{
    // 用户更新一般信息
    public function update()
    {
        // 接收数据并校验
        $data = Request::only(['username', 'password', 'email', 'mobile']);
        $validate =     hinkacadeValidate::rule([
            'username' => 'require|length:6,20|unique:user',
            'password' => 'require|alphaDash|confirm',
            'email'    => 'require|email|unique:user',
            'mobile'   => 'mobile|unique:user'
        ])->message([
            'username.require' => '用户名不能为空',
            'username.length'  => '用户名长度为6-20个字符',
            'username.unique'  => '该用户名已存在',
            'password.require' => '密码不能为空',
            'password.alphaDash'  => '密码只能是字母、数字、下划线和破折号',
            'password.confirm' => '两次输入密码不一致',
            'email.require'    => '邮箱不能为空',
            'email.email'      => '邮箱格式不正确',
            'email.unique'     => '该邮箱已存在',
            'mobile.mobile'    => '手机号格式不正确',
            'mobile.unique'    => '该手机号已存在'
        ]);
        if (!$validate->check($data)) {
            return json([
                'code' => -1,
                'msg'  => $validate->getError(),
                'data' => []
            ]);
        }

        // 保存数据至数据库
        $user = new User($data);
        $user->save();

        return json([
            'code' => 0,
            'msg'  => '保存成功',
            'data' => []
        ]);
    }
}
Copy after login

In the above controller, we use facade to receive and verify data. Since we do not use the previously defined User model here, the data validation rules and error messages are defined in the validate() function. If verification fails, we can return the corresponding error message. If the verification is successful, we save the data to the database and return a success message.

4. Summary

The above is the detailed process of using ThinkPHP6 to implement form verification. In actual development, form validation is a very important and common function. Reasonable form validation can improve the user experience and security of the system. ThinkPHP6 provides a very convenient and customizable form verification mechanism. We can make corresponding adjustments according to actual development needs to achieve more efficient and flexible form verification.

The above is the detailed content of How to use ThinkPHP6 to implement form validation. 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 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)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

PHP Programming Guide: Methods to Implement Fibonacci Sequence

See all articles