简体   繁体   English

如何对 antd Text (typography) 组件进行验证?

[英]how to do validation for a antd Text (typography) component?

It seems that we couldn't add validation for antd editable <Text> (typography).似乎我们无法为 antd editable <Text> (typography) 添加验证。 The component doesn't accept value props like <Input> component.该组件不接受像<Input>组件这样的value道具。 How do we do validation in that case?在这种情况下我们如何进行验证?

Code:代码:

const [title, setTitle] = useState("Battle");
<>
  <Form id="myForm" name="basic" onFinish={onSubmit}>
    <Form.Item
      name="title"
      rules={[
        {
          required: true,
          message: "Please input your title!"
        }
      ]}
    >
      <Text editable>{title}</Text>
    </Form.Item>
  </Form>

  <Button form="myForm" key="formSubmit" htmlType="submit" type="primary">
    Create
  </Button>
</>

Above code validation works but if the data is still there, it show validation error.上面的代码验证有效,但如果数据仍然存在,则显示验证错误。

Codesandbox link: https://codesandbox.io/s/basic-antd-4-16-9-forked-o8nxdl?file=/index.js Codesandbox 链接: https://codesandbox.io/s/basic-antd-4-16-9-forked-o8nxdl?file=/index.js

Here is a code that does some simple validations这是一个执行一些简单验证的代码

function reducer (action , state) {
  switch (action.type){
    case 'title': 
       return {...state , data : { ...state.data , title : action.payload } 
              , errors : {  ...state.errors 
          , title : title.length > 3 null : '😈'} } // title length must be > 3
  }
}

const [state , dispatch] = useReducer (reducer , {data : {} , errors : {})

console.log (state)

return ( <> <Form.Item
  name="title"
  rules={[
    {
      required: true,
      message: "Please input your title!"
    }
  ]}
  onChange = {(e) => dispatch ({type : 'title' , payload : e.target.value})
>
  <Text editable>{title}</Text>
</Form.Item> </>)

Before submitting make sure all errors must be null在提交之前确保所有错误必须是 null

you can handle it using the onChange like this working demo is here你可以使用onChange来处理它,就像这个工作演示在这里

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Form, Button, Input, Typography } from "antd";
import SecForm from "./SecForm";
const { Text } = Typography;

const App = () => {
  const [title, setTitle] = useState('Battle');
  const data = ["1", "2", "3"];

  const onHandleChange = (event) => {
    setTitle(event);
  };
  const onSubmit = (e) => {
    e.preventDefault();
    console.log("Submitted");
  };

  return (
    <>
      <Form id="myForm" name="basic" onFinish={onSubmit}>
        <Form.Item
          label="name"
          name="name"
          rules={[
            {
              required: true,
              message: "Please input your name!"
            }
          ]}
        >
          <Input placeholder="Name" />
        </Form.Item>

        <Form.Item
          label="rank"
          name="rank"
          rules={[
            {
              required: true,
              message: "Please input your rank!"
            }
          ]}
        >
          <Input placeholder="Rank" />
        </Form.Item>

        <Form.Item
          name="title"
          valuePropName={title}
          rules={[
            {
              required: title?.length < 1,
              message: "Please input your title!"
            }
          ]}
        >
          <Text editable={{ onChange: onHandleChange }}>{title}</Text>
        </Form.Item>
        {data.map((d, index) => (
          <SecForm index={index} />
        ))}
      </Form>

      <Button form="myForm" key="formSubmit" htmlType="submit" type="primary">
        Create
      </Button>
    </>
  );
};

ReactDOM.render(<App />, document.getElementById("container"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM