我无法通过单击按钮而不是提交来仅验证(不提交)表单,这是一个 Antd 表单。让它验证和更新有错误的字段的唯一方法是我尝试提交它。有没有办法做到这一点?还是我不得不提交表格?
我有一个提交按钮和一个仅用于验证的按钮 - 仅验证按钮调用 validateForm 方法,但表单上没有任何更新。
编辑:这里有一个指向代码和框的链接来演示这一点:https ://codesandbox.io/s/xo5ln7l32p ...再次 - 当我触摸文本框或单击提交按钮时,验证工作并在文本框下方显示错误消息,但是,当单独单击 Validate All 时,文本框下方不会显示任何错误。
这是我的基本 Antd 表单的代码:
import React from 'react';
import PropTypes from 'prop-types';
import { Form as AntdForm } from 'antd';
import FormValidationAlert from './FormValidationAlert';
function Form({ children, onSubmit, isValid, validationErrors }) {
return (
<AntdForm layout="vertical" onSubmit={onSubmit} style={{ margin: 20 }}>
{!isValid && <FormValidationAlert validationErrors={validationErrors} />}
{children}
</AntdForm>
);
}
Form.propTypes = {
onSubmit: PropTypes.func.isRequired,
isValid: PropTypes.bool.isRequired,
validationErrors: PropTypes.array
};
export default Form;
这是我的表格:
import React from 'react';
import PropTypes from 'prop-types';
import { Formik } from 'formik';
import * as Yup from 'yup';
import { Col, Row, Steps } from 'antd';
import {
Form,
TextInput
} from '../common/forms';
class CreateItemForm extends React.Component {
render() {
const formik = {
initialValues: {
name: ''
},
validationSchema: Yup.object().shape({
name: Yup.string().required('Name is required.')
}),
onSubmit: (values, actions) => {
this.props.onSubmit(values);
}
};
console.log('this.props', this.props);
const { setFormRef, status, currentStep } = this.props;
return (
<Formik
ref={setFormRef}
{...formik}
render={form => (
<Form
onSubmit={form.handleSubmit}
isValid={status.isValid}
validationErrors={status.validationErrors}
>
<TextInput
{...form}
name="name"
placeholder="Name"
label="Name"
/>
<button type="button" onClick={() => validateForm().then(() => console.log(blah))}>
Validate All
</button>
<button type="submit">Submit</button>
</ Form>
)}
/>
);
}
}
CreateItemForm.propTypes = {
onSubmit: PropTypes.func.isRequired,
status: PropTypes.object.isRequired,
setFormRef: PropTypes.func.isRequired
};
export default CreateItemForm;
这是我的文本输入:
import React from 'react';
import PropTypes from 'prop-types';
import { Form, Input } from 'antd';
import ReactInputMask from 'react-input-mask';
function TextInput({
values,
errors,
touched,
handleSubmit,
setFieldValue,
setFieldTouched,
name,
label,
placeholder,
disabled,
addOnBeforeValue,
addOnAfterValue,
mask,
maskPermanents
}) {
return (
<Form.Item
label={label}
hasFeedback={!!errors[name]}
validateStatus={touched[name] && errors[name] && 'error'}
help={touched[name] && errors[name]}
>
{mask ? (
<ReactInputMask
disabled={
disabled === null || disabled === undefined ? false : disabled
}
alwaysShowMask={false}
value={values[name]}
onChange={event => setFieldValue(name, event.target.value)}
onBlur={() => setFieldTouched(name)}
mask={mask}
permanents={maskPermanents}
>
{inputProps => (
<Input
{...inputProps}
placeholder={placeholder}
onPressEnter={handleSubmit}
addonBefore={addOnBeforeValue}
addonAfter={addOnAfterValue}
/>
)}
</ReactInputMask>
) : (
<Input
disabled={disabled}
placeholder={placeholder}
value={values[name]}
onChange={event => setFieldValue(name, event.target.value)}
onBlur={() => setFieldTouched(name)}
onPressEnter={handleSubmit}
addonBefore={addOnBeforeValue}
addonAfter={addOnAfterValue}
/>
)}
</Form.Item>
);
}
TextInput.propTypes = {
values: PropTypes.object,
errors: PropTypes.object,
touched: PropTypes.object,
handleSubmit: PropTypes.func,
setFieldValue: PropTypes.func,
setFieldTouched: PropTypes.func,
name: PropTypes.string.isRequired,
label: PropTypes.string,
disabled: PropTypes.bool,
placeholder: PropTypes.string,
addOnBeforeValue: PropTypes.string,
addOnAfterValue: PropTypes.string,
mask: PropTypes.string,
maskPermanents: PropTypes.arrayOf(PropTypes.number)
};
export default TextInput;