


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.
- Rule definition and grouping: Do not place all rules directly in
rules
property. We can add atrigger
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' }, ];
- 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 (
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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�...

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

redis...

Using python in Linux terminal...

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

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

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

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...
