如何在ANT设计的某些字段中设置选定的值,以使其与ReactJ中的验证规则保持兼容?

发布于 2025-02-05 01:29:40 字数 4929 浏览 1 评论 0原文

我正在使用use antd设计模板,带有antd版本“ antd”:“^4.16.6”

我有一个蚂蚁设计表格,在提交表格以编辑现有记录之前,我正在填充数据。

CurrencyConversionById是我要编辑的对象/记录。它的结构就是这样: -

currencyConversionById = {
    base_currency   : "USD",
    base_amount     : 1,
    to_currency     : "CAD",
    to_amount       : 2.45
}

货币清单是这样的货币列表: -

currencyList = [
    {
        currency        : "American Dollar",
        currency_code   : "USD"
    },
    {
        currency        : "Canadian Dollar",
        currency_code   : "CAD"
    }
]

选择字段将其货币列表作为其选项和与CORGEN> CORMEN> CORMING> CORLUND CORMINGERYBYID.TO_Currency.to_currency 的选项。 ,将显示为选定。

这是形式: -

<Form {...layout} form={form} name={formTitle(window.location.pathname)} onFinish={onFinish}>
    <Form.Item name="base_currency" label="Base Currency" rules={CURRENCY_CODE_VALIDATION}>
    <Input value={baseCurrency} onChange={({ target: { value, } }) => setBaseCurrency(value)} readOnly={true}/>
    </Form.Item>
    <Form.Item name="base_amount" label="Base Amount">
    <Input value={baseCurrencyAmount} onChange={({ target: { value, } }) => setBaseCurrencyAmount(value)} readOnly={true}/>
    </Form.Item>
    {
    currencyListLoading
    ? <Loader message={"Loading Currency List"} description={"Listing all currency data"} type={"success"}/>
    : <Form.Item name="toCurrency" label="To Currency" rules={[{ required: true, message: 'Please, select a secondary currency' }]} initialvalue={toCurrency}>
        <Select placeholder="Select a option and change input text above" allowClear onChange={value => setToCurrency(value)} onLoad={value => setToCurrency(value)} value={toCurrency} defaultValue={toCurrency}>
            <Option key={0} value="0">None</Option>
            {currencyList && currencyList.length > 0 && currencyList.map((currency, indexCrn) => {
            return (
                <Option key={currency?.currency_code} value={currency?.currency_code}>{currency?.currency_code}</Option>
                )
            })}
        </Select>
        </Form.Item>
    }
    <Form.Item name="to_amount" label="To Amount" rules={AMOUNT_VALIDATION}>
    <Input value={toCurrencyAmount} onChange={value => setToCurrencyAmount(value)} onLoadedData={value => setToCurrencyAmount(value)}/>
    </Form.Item>
    <Form.Item {...tailLayout}>
    <Button type="primary" htmlType="submit">{formCardTitle(window.location.pathname)}</Button> &nbsp;
    <Button htmlType="button" onClick={onReset}>
        Reset
    </Button>
    </Form.Item>
</Form>

在形式中填充数据时,我做到了: -

  useEffect(() => {
  if (currencyConversionById !== null)
  {
    form.setFieldsValue({
      base_currency : currencyConversionById?.base_currency,
      base_amount   : currencyConversionById?.base_amount,
      to_currency   : currencyConversionById?.to_currency.toString(),
      to_amount     : currencyConversionById?.to_amount.toString()
    });
    setToCurrency(currencyConversionById?.to_currency);
  }  
}, [currencyConversionById])

使用form.setfieldsvalue我可以为所有文本输入设置值,但是对于Select此: -

const [toCurrency, setToCurrency]  = useState("");

useEffect(() => {
    if (currencyConversionById !== null)
    {
        ------------------------
        ------------------------
        setToCurrency(currencyConversionById?.to_currency);
    }  
}, [currencyConversionById])

使用settocurrency(),我设置了tocurrency值为“ cad”,然后以表格,传递为initialValuedefaultValue这样: -

<Form.Item name="toCurrency" label="To Currency" rules={[{ required: true, message: 'Please, select a secondary currency' }]} initialvalue={toCurrency}>
<Select placeholder="Select a option and change input text above" allowClear onChange={value => setToCurrency(value)} onLoad={value => setToCurrency(value)} value={toCurrency} defaultValue={toCurrency}>
    <Option key={0} value="0">None</Option>
    {currencyList && currencyList.length > 0 && currencyList.map((currency, indexCrn) => {
    return (
        <Option key={currency?.currency_code} value={currency?.currency_code}>{currency?.currency_code}</Option>
        )
    })}
</Select>
</Form.Item>

通过这样做,“ CAD”显示为选定选项。但是,当我尝试提交功能时,触发了to_currency的验证规则。

为什么这样发生?由于已经显示为选定的选项,因此不应触发所需的验证。但是不知道为什么这种验证一直被触发。

我该如何解决?

I am using Muse Antd Design Template with antd version "antd": "^4.16.6".

I have an ant design form where I am populating data before submitting the form to edit an existing record.

currencyConversionById is the object/record I want to edit. Its structure is like this:-

currencyConversionById = {
    base_currency   : "USD",
    base_amount     : 1,
    to_currency     : "CAD",
    to_amount       : 2.45
}

currencyList is list of currencies like this:-

currencyList = [
    {
        currency        : "American Dollar",
        currency_code   : "USD"
    },
    {
        currency        : "Canadian Dollar",
        currency_code   : "CAD"
    }
]

The select field will have the currency list as its options and the option which matches with currencyConversionById.to_currency, will be displayed as selected.

This is the form:-

<Form {...layout} form={form} name={formTitle(window.location.pathname)} onFinish={onFinish}>
    <Form.Item name="base_currency" label="Base Currency" rules={CURRENCY_CODE_VALIDATION}>
    <Input value={baseCurrency} onChange={({ target: { value, } }) => setBaseCurrency(value)} readOnly={true}/>
    </Form.Item>
    <Form.Item name="base_amount" label="Base Amount">
    <Input value={baseCurrencyAmount} onChange={({ target: { value, } }) => setBaseCurrencyAmount(value)} readOnly={true}/>
    </Form.Item>
    {
    currencyListLoading
    ? <Loader message={"Loading Currency List"} description={"Listing all currency data"} type={"success"}/>
    : <Form.Item name="toCurrency" label="To Currency" rules={[{ required: true, message: 'Please, select a secondary currency' }]} initialvalue={toCurrency}>
        <Select placeholder="Select a option and change input text above" allowClear onChange={value => setToCurrency(value)} onLoad={value => setToCurrency(value)} value={toCurrency} defaultValue={toCurrency}>
            <Option key={0} value="0">None</Option>
            {currencyList && currencyList.length > 0 && currencyList.map((currency, indexCrn) => {
            return (
                <Option key={currency?.currency_code} value={currency?.currency_code}>{currency?.currency_code}</Option>
                )
            })}
        </Select>
        </Form.Item>
    }
    <Form.Item name="to_amount" label="To Amount" rules={AMOUNT_VALIDATION}>
    <Input value={toCurrencyAmount} onChange={value => setToCurrencyAmount(value)} onLoadedData={value => setToCurrencyAmount(value)}/>
    </Form.Item>
    <Form.Item {...tailLayout}>
    <Button type="primary" htmlType="submit">{formCardTitle(window.location.pathname)}</Button>  
    <Button htmlType="button" onClick={onReset}>
        Reset
    </Button>
    </Form.Item>
</Form>

While populating the data in the form, I did this:-

  useEffect(() => {
  if (currencyConversionById !== null)
  {
    form.setFieldsValue({
      base_currency : currencyConversionById?.base_currency,
      base_amount   : currencyConversionById?.base_amount,
      to_currency   : currencyConversionById?.to_currency.toString(),
      to_amount     : currencyConversionById?.to_amount.toString()
    });
    setToCurrency(currencyConversionById?.to_currency);
  }  
}, [currencyConversionById])

Using form.setFieldsValue I could set the values for all the text inputs, but for Select option, I had to do this:-

const [toCurrency, setToCurrency]  = useState("");

useEffect(() => {
    if (currencyConversionById !== null)
    {
        ------------------------
        ------------------------
        setToCurrency(currencyConversionById?.to_currency);
    }  
}, [currencyConversionById])

Using setToCurrency(), I set up toCurrency value as "CAD", and then in the form, passed it was initialValue and defaultValue like this:-

<Form.Item name="toCurrency" label="To Currency" rules={[{ required: true, message: 'Please, select a secondary currency' }]} initialvalue={toCurrency}>
<Select placeholder="Select a option and change input text above" allowClear onChange={value => setToCurrency(value)} onLoad={value => setToCurrency(value)} value={toCurrency} defaultValue={toCurrency}>
    <Option key={0} value="0">None</Option>
    {currencyList && currencyList.length > 0 && currencyList.map((currency, indexCrn) => {
    return (
        <Option key={currency?.currency_code} value={currency?.currency_code}>{currency?.currency_code}</Option>
        )
    })}
</Select>
</Form.Item>

By doing this, "CAD" is displayed as selected option. However, when I am trying to submit the function, the validation rule for to_currency is triggered.

enter image description here

Why is it happening like this? Since an option is already shown as selected, the required validation should not be fired. But don't know why this validation is keep on getting triggered.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-02-12 01:29:40

您可以使用initialValues表格属性来设置初始值

form.Item带有名称属性的组件将变成
受控模式,这使默认值不再起作用。请尝试
表单的初始价值设置默认值。

You can use initialValues form property to set initial values

Components inside Form.Item with name property will turn into
controlled mode, which makes defaultValue not work anymore. Please try
initialValues of Form to set default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文