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

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-04 14:41:32 +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 14:41:32 +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 validate onBlur && onChange</div>
<div>
<input
{...getFieldProps('email', {
validate: [
{
trigger: 'blur',
rules: [
{
required: true,
},
],
},
{
trigger: ['blur', 'input'],
rules: [
{
type: 'email',
message: '错误的 email 格式',
},
],
},
],
})}
/>
</div>
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
<div style={errorStyle}>{isFieldValidating('email') ? 'validating' : null}</div>
2018-05-04 14:41:32 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 14:41:32 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 14:41:32 +00:00
const User = {
props: {
form: Object,
},
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 validate on submit</div>
<div>
<input
{...getFieldProps('user', {
rules: [
{
required: true,
},
{
type: 'string',
min: 5,
},
],
validateTrigger: null,
})}
/>
</div>
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
<div style={errorStyle}>{isFieldValidating('user') ? 'validating' : null}</div>
2018-05-04 14:41:32 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 14:41:32 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 14:41:32 +00:00
const Form = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
e.preventDefault();
2018-05-04 14:41:32 +00:00
this.form.validateFields((error, values) => {
if (!error) {
2019-01-12 03:33:27 +00:00
console.log('ok', values);
2018-05-04 14:41:32 +00:00
} else {
2019-01-12 03:33:27 +00:00
console.log('error', error, values);
2018-05-04 14:41:32 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-05-04 14:41:32 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { form } = this;
return (
<div style={{ margin: '20px' }}>
<h2>use validateTrigger config</h2>
<form onSubmit={this.onSubmit}>
<User form={form} />
2018-05-04 14:41:32 +00:00
2019-01-12 03:33:27 +00:00
<Email form={form} />
2018-05-04 14:41:32 +00:00
2019-01-12 03:33:27 +00:00
<div style={regionStyle}>
<button>submit</button>
</div>
</form>
</div>
);
2018-05-04 14:41:32 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 14:41:32 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);