(component): 新增 form-item 组件 requiredErrorMessage 属性, 允许用户自定义 required 错误提示信息

This commit is contained in:
就眠儀式 2022-09-09 01:40:43 +08:00
parent 6b8f0cc331
commit d3609d636d
2 changed files with 17 additions and 17 deletions

View File

@ -37,6 +37,7 @@ export interface LayFormItemProps {
errorMessage?: string;
rules?: Rule;
required?: boolean;
requiredErrorMessage?: string;
}
const props = withDefaults(defineProps<LayFormItemProps>(), {
@ -84,26 +85,22 @@ watch(
);
//
const errorStatus = ref(false);
const errorMsg = ref();
const errorStatus = ref(false);
//
const validate = (callback?: ValidateCallback) => {
if (props.prop && (ruleItems.value as RuleItem[]).length > 0) {
//
const descriptor: Rules = {};
descriptor[layForm.useCN ? props.label || props.prop : props.prop] =
ruleItems.value;
descriptor[layForm.useCN ? props.label || props.prop : props.prop] = ruleItems.value;
const validator = new Schema(descriptor);
let model: { [key: string]: any } = {};
let validateMessage = null;
// 使
if (layForm.useCN) {
validateMessage = Object.assign(
{},
cnValidateMessage,
layForm.validateMessage
);
validateMessage = Object.assign({}, cnValidateMessage, layForm.validateMessage);
model[props.label || props.prop] = filedValue.value;
} else {
layForm.validateMessage && (validateMessage = layForm.validateMessage);
@ -112,11 +109,14 @@ const validate = (callback?: ValidateCallback) => {
//
layForm.requiredErrorMessage &&
// @ts-ignore
(validateMessage = Object.assign(validateMessage, {
required: layForm.requiredErrorMessage,
}));
validateMessage && validator.messages(validateMessage);
(validateMessage = Object.assign(validateMessage, { required: layForm.requiredErrorMessage }));
props.requiredErrorMessage &&
// @ts-ignore
(validateMessage = Object.assign(validateMessage, { required: props.requiredErrorMessage }));
validateMessage && validator.messages(validateMessage);
//
validator.validate(model, (errors, fields) => {
errorStatus.value = errors !== null && errors.length > 0;
@ -130,8 +130,8 @@ const validate = (callback?: ValidateCallback) => {
error.field = props.prop;
});
errorMsg.value = props.errorMessage ?? _errors[0].message;
slotParentDiv.childElementCount > 0 &&
slotParentDiv.firstElementChild?.classList.add("layui-form-danger");
slotParentDiv?.childElementCount > 0 &&
slotParentDiv?.firstElementChild?.classList.add("layui-form-danger");
callback && callback(_errors, fields);
} else {
clearValidate();
@ -139,7 +139,7 @@ const validate = (callback?: ValidateCallback) => {
});
}
};
//
const clearValidate = () => {
errorStatus.value = false;

View File

@ -241,7 +241,7 @@ export default {
<template>
<lay-form :model="ruleDemo1" ref="layFormRef1" :rules="rules" required initValidate>
<lay-form-item label="邮箱" prop="email">
<lay-form-item prop="email" requiredMessage="你得输入邮箱">
<lay-input v-model="ruleDemo1.email"></lay-input>
</lay-form-item>
<lay-form-item label="url路径" prop="url">
@ -279,7 +279,7 @@ export default {
const rules = ref({
email : {
type : 'email'
type : 'email',
},
url : {
type : 'url'