Я хочу проверить метод из вложенного компонента. Как я могу это сделать?
Это мой код:
const detailValidation = detail => {
const errors = {};
if (!detail || !detail.length) {
return { _error: "At least one detail item must be required" };
} else {
let detailArrayErrors = [];
detail.forEach((detailItem, detailIndex) => {
const detailErrors = {
quantity: null,
product: { code: null, name: null, price: null },
subtotal: null
};
if (!detailItem || !detailItem.quantity) {
detailErrors.quantity = "required";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || isNaN(detailItem.quantity)) {
detailErrors.quantity = "you must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !/^([0-9.])/.test(detailItem.quantity)) {
detailErrors.quantity = "you must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
}
if (!detail || !detailItem.product || !detailItem.product.code) {
detailErrors.product.code = "you must entry code";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !detailItem.product || isNaN(detailItem.product.code)) {
detailErrors.product.code = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !detailItem.product || !/^([0-9.])/.test(detailItem.product.code)) {
detailErrors.product.code = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
}
if (!detailItem || !detailItem.product || !detailItem.product.name) {
detailErrors.product.name = "required";
detailArrayErrors[detailIndex] = detailErrors;
}
if (!detailItem || !detailItem.product || !detailItem.product.price) {
detailErrors.product.price = "required";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !detailItem.product || isNaN(detailItem.product.price)) {
detailErrors.product.price = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !detailItem.product || !/^([0-9.])/.test(detailItem.product.price)) {
detailErrors.product.price = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
}
if (!detailItem || !detailItem.subtotal) {
detailErrors.subtotal = "required";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || isNaN(detailItem.subtotal)) {
detailErrors.subtotal = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
} else if (!detailItem || !/^([0-9.])/.test(detailItem.subtotal)) {
detailErrors.subtotal = "You must entry numbers";
detailArrayErrors[detailIndex] = detailErrors;
}
});
if (detailArrayErrors.length) {
errors.detail = detailArrayErrors;
}
}
return errors;
};
Это когда я проверяю методы валидации в избыточной форме:
DetailFormBill = reduxForm({
form: 'bill',
validate: detailValidation
})(DetailFormBill);
Я экспортирую DetailFormBill и detailValidation export {detailValidation, DetailFormBill}
Теперь я нахожусь здесь в BillForm.jsx, вызывая detailValidation:
import {DetailFormBill,detailValidation} from './DetailFormBill';
const validations = values => {
const errors = {};
errors.detail = detailValidation(values.detail)
return errors;
}
Ожидаемый результат - увидеть проверки, но реальный результат - «Я могу» «Не вижу проверок», возможно из-за синтаксической ошибки, кроме случаев, когда
if(!detail || !detail.length){
return { _error:'At least one detail item must be required' }
}
Что не так? Мне нужно решить эту проблему.