feat(Form): add show-error-message prop (#5927)

This commit is contained in:
neverland 2020-03-27 15:26:16 +08:00 committed by GitHub
parent 8dd5d9aead
commit 529457cbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 1 deletions

View File

@ -474,6 +474,10 @@ export default createComponent({
},
genMessage() {
if (this.vanForm && this.vanForm.showErrorMessage === false) {
return;
}
const message = this.errorMessage || this.validateMessage;
if (message) {

View File

@ -431,7 +431,8 @@ export default {
| validate-trigger `v2.5.2` | When to validate the formcan be set to `onChange``onSubmit` | *string* | `onBlur` |
| colon | Whether to display `:` after label | *boolean* | `false` |
| validate-first | Whether to stop the validation when a rule fails | *boolean* | `false` |
| scroll-to-error `v2.5.2` | Whether to scroll to the error field when submit failed | *boolean* | `false` |
| scroll-to-error `v2.5.2` | Whether to scroll to the error field when validation failed | *boolean* | `false` |
| show-error-message `v2.5.8` | Whether to show error message when validation failed | *boolean* | `true` |
### Data Structure of Rule

View File

@ -467,6 +467,7 @@ export default {
| colon | 是否在 label 后面添加冒号 | *boolean* | `false` |
| validate-first | 是否在某一项校验不通过时停止校验 | *boolean* | `false` |
| scroll-to-error `v2.5.2` | 是否在提交表单且校验不通过时滚动至错误的表单项 | *boolean* | `false` |
| show-error-message `v2.5.8` | 是否在校验不通过时在输入框下方展示错误提示 | *boolean* | `true` |
> 表单项的 API 参见:[Field 组件](#/zh-CN/field#api)

View File

@ -15,6 +15,10 @@ export default createComponent({
type: String,
default: 'onBlur',
},
showErrorMessage: {
type: Boolean,
default: true,
},
},
provide() {

View File

@ -381,3 +381,28 @@ test('scroll-to-error prop', async () => {
expect(fn).toHaveBeenCalledTimes(1);
});
test('show-error-message prop', async () => {
const wrapper = mountForm({
template: `
<van-form :show-error-message="showErrorMessage">
<van-field name="A" :rules="rulesA" value="" />
<van-button native-type="submit" />
</van-form>
`,
data() {
return {
...getSimpleRules(),
showErrorMessage: false,
};
},
});
await submitForm(wrapper);
expect(wrapper.contains('.van-field__error-message')).toBeFalsy();
wrapper.setData({ showErrorMessage: true });
await submitForm(wrapper);
expect(wrapper.contains('.van-field__error-message')).toBeTruthy();
});