这是我的验证:
const validateMutation = [required(), number(), minValue(1)];
and this is my field:
<NumberInput
variant="outlined"
source={mutation}
label="Amount"
fullWidth
isRequired
validate={validateMutation}
format={(v) => {
if (v === "") return "";
if (v === undefined) return undefined;
return v * -1;
}}
parse={(v) => {
if (v === "") return "";
return v * -1;
}}
/>
基本上我从api得到了负值的mutation
,我将其转换为要在字段中显示的正值,并以正值提交,但问题是当我提交表单失败时,突变字段下面的错误说:
必须至少为1
似乎这个验证minValue(1)
仍然持有来自api的负值,如何使这个验证minValue(1)
接收已经在正值的解析或格式化的值?
确实,验证检查应用于已解析的值。
如果您想对格式化的值执行检查,则需要在验证它之前对值重新应用format
函数,例如:
const applyFormat = (format, validator) => (value) => validator(format(value));
const formatFn = (v) => {
if (v === '') return '';
if (v === undefined) return undefined;
return v * -1;
};
const validateMutation = [
required(),
number(),
applyFormat(formatFn, minValue(1)),
];