如何在ANT设计的某些字段中设置选定的值,以使其与ReactJ中的验证规则保持兼容?
我正在使用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>
<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”
,然后以表格,传递为initialValue
和defaultValue
这样: -
<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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
initialValues
表格属性来设置初始值You can use
initialValues
form property to set initial values