gpt4 book ai didi

reactjs - 如何在 React 中添加对状态码 400 的验证

转载 作者:行者123 更新时间:2023-12-05 07:03:25 26 4
gpt4 key购买 nike

我正在尝试编辑一个软件程序,该程序需要验证“未捕获( promise )错误:请求失败,状态代码为 400”错误

原因是用户试图在表上创建相同的唯一数据。这就是它获得状态代码 400 的原因。

现在,我想为状态代码 400 设置一个错误处理程序,并将在文本字段中返回一个字符串,说明“重复输入”

这是我当前的验证。

    import * as Yup from "yup";

const InvoiceValidationSchema = Yup.object().shape({
customer: Yup.object()
.required("Customer is required."),
invoiceDate: Yup.string()
.required("Invoice Date is required"),
dueDate: Yup.string()
.required("Due Date is required."),
invoiceNumber: Yup.string()
.required("Invoice Number is required"),
invoiceLines: Yup.array()
.of(
Yup.object().shape({
product: Yup.object().required("Required"),
quantity: Yup.number().required("Required.").min(1, "Quantity cannot be zero."),
// price: Yup.number().required("Required.").min(1, "Quantity cannot be zero.").typeError("")
})
).required("Required")
});

export default InvoiceValidationSchema;

我希望有人能帮助我。谢谢

这是表格

<Formik
validationSchema={InvoiceValidationSchema}
initialValues={invoiceCrud}
onSubmit={(values, action) => {

const invoiceLines = values.invoiceLines.map(invoiceLine => {
return {
...invoiceLine,
product: invoiceLine.product,
price: invoiceLine.price,
quantity: invoiceLine.quantity,
totalAmount: invoiceLine.quantity * invoiceLine.price,
averagePurchasePrice: invoiceLine.product.averagePurchasePrice
}
})

const subTotal = invoiceLines.reduce((sum, line) => {
return sum + line.totalAmount;
},0)

const totalExpense = invoiceLines.reduce((sum, line) => {
return sum + (line.product.averagePurchasePrice * line.quantity);
},0)

const totalAdjustment = 0;
const netTotal = subTotal - totalAdjustment;

const totalDueAmount = subTotal - values.subTotal + values.totalDueAmount;


const invoice ={
...values,
invoiceLines,
subTotal,
totalAdjustment,
netTotal,
totalExpense,
totalDueAmount
}

if(values.invoiceLines[0].price > 0 && values.invoiceLines[0].quantity > 0){
submit(invoice);
setTimeout(() => {
action.setSubmitting(false);
}, 1000);
}else{
setIsZero(true)
action.setSubmitting(false)
}
}}
render={
({ values, errors, touched, setFieldValue, isSubmitting }) => (
<Form>
<div className="box-body" id="invoice-crud">
<div className="row">
<div className="col-lg-3 col-md-6 col-sm-12">
{errors.invoiceLines ? setIsProductSelected(false) : null}
{errors.customer ? setCustomerClass("btn btn-default dropdown-toggle transaction-form__account has-error")
: setCustomerClass("btn btn-default dropdown-toggle transaction-form__account")}
<DropdownWithSearch
property={
{
title: "Customer",
buttonClass: CustomerClass,
buttonLabel: values.customer ? values.customer.partnerName : "Select Customer",
newButtonLabel: "Customer",
showRemove: values.customer,
modalSelector: "#partner-form"
}
}
option={{
list: partnerDetails.list,
total: partnerDetails.total,
currentPage: partnerDetails.currentPage,
pageSize: partnerDetails.pageSize,
load: changePartnerPage,
display: (element) => element.partnerName,
onClick: (element) => {
setFieldValue("customer", element)
},
removeSelected: () => {
setFieldValue("customer", null)
},
search: (search) => {
dropdownSearch(search, "partner")
}
}}
/>{errors.customer ? <span className="errorMessage">Please select a customer</span> : null}
</div>
<FormRow validation={{errors: errors, touched: touched}} field={invoiceNumber}/>
</Form>
)}
</Formik>

最佳答案

问题似乎与 Formik 和处理后端错误有关,而不是其他任何问题。

您可以在 this github issue 中查看如何在 formik 中手动设置后端错误.

特别是 this sandbox ,由用户 @t-nunes 在 github 中发布。

如果发出请求的函数是以新的 async/await 风格编写的,您可能会在 try/catch block 中处理错误,并且对于旧的 .then.catch 样式,只要服务器返回错误代码,您就可以使用 .catch 回调。

关于reactjs - 如何在 React 中添加对状态码 400 的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63231415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com