# 表单校验 - order: 11 基本的表单校验例子。 --- ````jsx import { Button, Form, Input } from 'antd'; const createForm = Form.create; const FormItem = Form.Item; function noop() { return false; } class BasicDemo extends React.Component { getValidateStatus(field) { const { isFieldValidating, getFieldError, getFieldValue } = this.props.form; if (isFieldValidating(field)) { return 'validating'; } else if (!!getFieldError(field)) { return 'error'; } else if (getFieldValue(field)) { return 'success'; } } handleReset(e) { e.preventDefault(); this.props.form.resetFields(); } handleSubmit(e) { e.preventDefault(); this.props.form.validateFields((errors, values) => { if (!!errors) { console.log('Errors in form!!!'); return; } console.log('Submit!!!'); console.log(values); }); } userExists(rule, value, callback) { if (!value) { callback(); } else { setTimeout(() => { if (value === 'JasonWood') { callback([new Error('抱歉,该用户名已被占用。')]); } else { callback(); } }, 800); } } checkPass(rule, value, callback) { const { validateFields } = this.props.form; if (value) { validateFields(['rePasswd']); } callback(); } checkPass2(rule, value, callback) { const { getFieldValue } = this.props.form; if (value && value !== getFieldValue('passwd')) { callback('两次输入密码不一致!'); } else { callback(); } } render() { const { isFieldValidating, getFieldError } = this.props.form; return (
); } } BasicDemo = createForm({})(BasicDemo); ReactDOM.render(