ant-design-vue/components/vc-form/demo/validateFirst.js

134 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-05-04 11:11:42 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
2019-01-12 03:33:27 +00:00
import { createForm } from '../index';
import { regionStyle, errorStyle } from './styles';
2018-05-04 11:11:42 +00:00
const Email = {
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
const errors = getFieldError('email');
return (
<div style={regionStyle}>
<div>email sync validate</div>
<div>
<input
{...getFieldProps('email', {
initialValue: '',
validateFirst: true,
rules: [
{
required: true,
},
{
type: 'email',
message: '错误的 email 格式',
},
],
})}
/>
</div>
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
<div style={errorStyle}>{isFieldValidating('email') ? 'validating' : null}</div>
2018-05-04 11:11:42 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
const User = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
userExists(rule, value, callback) {
2018-05-04 11:11:42 +00:00
setTimeout(() => {
if (value === '1') {
2019-01-12 03:33:27 +00:00
callback([new Error('are you kidding?')]);
2018-05-04 11:11:42 +00:00
} else if (value === 'yiminghe') {
2019-01-12 03:33:27 +00:00
callback([new Error('forbid yiminghe')]);
2018-05-04 11:11:42 +00:00
} else {
2019-01-12 03:33:27 +00:00
callback();
2018-05-04 11:11:42 +00:00
}
2019-01-12 03:33:27 +00:00
}, 300);
2018-05-04 11:11:42 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
const errors = getFieldError('user');
return (
<div style={regionStyle}>
<div>user async validate</div>
<div>
<input
{...getFieldProps('user', {
initialValue: '',
rules: [
{
required: true,
min: 2,
},
{
validator: this.userExists,
},
],
})}
/>
</div>
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
<div style={errorStyle}>{isFieldValidating('user') ? 'validating' : null}</div>
2018-05-04 11:11:42 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
const Form = {
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
console.log('submit');
e.preventDefault();
this.form.validateFields(
{
// firstFields: false,
},
(error, values) => {
if (!error) {
console.log('ok', values);
} else {
console.log('error', error, values);
}
},
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
reset(e) {
e.preventDefault();
this.form.resetFields();
2018-05-04 11:11:42 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { form } = this;
return (
<div style={{ margin: '20px' }}>
<h2>validateFirst</h2>
<form onSubmit={this.onSubmit}>
<User form={form} />
2018-05-04 11:11:42 +00:00
2019-01-12 03:33:27 +00:00
<Email form={form} />
2018-05-04 11:11:42 +00:00
2019-01-12 03:33:27 +00:00
<div style={regionStyle}>
<button onClick={this.reset}>reset</button>
&nbsp;
<input type="submit" value="submit" />
</div>
</form>
</div>
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);