Home Web Front-end JS Tutorial Ant Design Form: How to set multiple verification rules for different triggering times for the same field?

Ant Design Form: How to set multiple verification rules for different triggering times for the same field?

Apr 04, 2025 pm 01:18 PM
red

Ant Design Form: How to set multiple verification rules for different triggering times for the same field?

Ant Design Form: How to set multiple verification rules for different triggering opportunities for the same field?

How does Ant Design 3's Form form set multiple check rules for a single field and specify their triggering timings (for example, onChange or onBlur )? This article will provide an efficient solution to avoid the complexity of custom components.

Suppose three check rules need to be set for a field: the first two are checked immediately when the value changes ( onChange ), and the last one is checked when the focus is lost ( onBlur ). Using rules attribute directly does not meet this requirement.

Solution:

The core idea is to group verification rules and use the form.validateFields method to manually trigger verification of specific rules in onBlur event.

  1. Rule definition and grouping: Do not place all rules directly in rules property. We can add a trigger attribute to each rule object, for example:
 const rules = [
  { required: true, message: 'required', trigger: 'onChange' },
  { pattern: /^[az] $/i, message: 'Only enter letters', trigger: 'onChange' },
  { min: 6, message: 'at least 6 characters', trigger: 'onBlur' },
];
Copy after login
  1. Conditional verification: In the form field component, listen for onBlur events:
 const MyComponent = ({ form }) => {
  const [fieldName] = useState('myField'); // The field name to be checked const handleBlur = () => {
    form.validateFields([fieldName])
      .then(() => {
        // Operation after successful verification})
      .catch((errorInfo) => {
        // Operation after verification failed});
  };

  Return (
    
Copy after login
rule.trigger === 'onChange')}> ); };

Note: rules attribute only contains the rules of trigger: 'onChange' , and the onBlur rule is triggered by form.validateFields method in the handleBlur function.

Through this method, we utilize the built-in verification mechanism of Ant Design Form and manually trigger validateFields to achieve multiple verifications at different trigger times, avoiding the problem of directly modifying the style and not affecting the form verification results, thus maintaining the integrity and flexibility of form verification.

The above is the detailed content of Ant Design Form: How to set multiple verification rules for different triggering times for the same field?. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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 send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to optimize Debian Hadoop How to optimize Debian Hadoop Apr 02, 2025 am 08:54 AM

To improve the performance of DebianHadoop cluster, we need to start from hardware, software, resource management and performance tuning. The following are some key optimization strategies and suggestions: 1. Select hardware and system configurations carefully to select hardware configurations: Select the appropriate CPU, memory and storage devices according to actual application scenarios. SSD accelerated I/O: Use solid state hard drives (SSDs) as much as possible to improve I/O operation speed. Memory expansion: Allocate sufficient memory to NameNode and DataNode nodes to cope with larger data processing and tasks. 2. Software configuration optimization Hadoop configuration file adjustment: core-site.xml: Configure HDFS default file system

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

How to efficiently start multiple services in Dockerfile? How to efficiently start multiple services in Dockerfile? Apr 01, 2025 pm 02:15 PM

About efficient use of CMD commands in Dockerfile Many new Docker users are using CMD...

Java calls Python Spark program to get stuck: How to solve the problem of Runtime.getRuntime().exec() blocking? Java calls Python Spark program to get stuck: How to solve the problem of Runtime.getRuntime().exec() blocking? Apr 01, 2025 pm 10:42 PM

Analysis and solution of problem of jammed calling Python code in Java. When calling Python code with Java, you often encounter some difficult problems, such as programs...

See all articles