Skip to content

Commit

Permalink
fix(Form): 修复表单嵌套时必填项样式设置的问题 (#499)
Browse files Browse the repository at this point in the history
* docs(Form): 文档增加嵌套验证案例

* fix(Form): 修复表单嵌套时必填项样式设置的问题
  • Loading branch information
ocean-gao authored Nov 1, 2023
1 parent e8785a4 commit 7827186
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 96 deletions.
12 changes: 8 additions & 4 deletions components/form/formItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,18 @@ export default defineComponent({
`${prefixCls}-span-${Math.ceil(props.span || span.value)}`,
labelPosition.value !== LABEL_POSITION.LEFT &&
`${prefixCls}-${labelPosition.value}`,
formItemRequired.value && 'is-required', // 必填校验: is-required
validateStatus.value === VALIDATE_STATUS.ERROR && 'is-error', // 校验错误: is-error
FORM_ITEM_ALIGN.includes(props.align) &&
`${prefixCls}-align-${props.align}`,
].filter(Boolean),
);
const formItemLabelClass = computed(() =>
[`${prefixCls}-label`, labelClass.value, props.labelClass].filter(
Boolean,
),
[
`${prefixCls}-label`,
formItemRequired.value && 'is-required', // 必填校验: is-required
labelClass.value,
props.labelClass,
].filter(Boolean),
);
const formItemLabelStyle = computed(() => ({
width: pxfy(props.labelWidth || labelWidth.value),
Expand Down Expand Up @@ -275,6 +277,8 @@ export default defineComponent({
validate,
clearValidate,
formItemRules,
};
},
});
Expand Down
4 changes: 2 additions & 2 deletions components/form/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@

.@{form-cls} {
// formItem 必填样式
.@{form-item-cls}.@{form-item-required} {
.@{form-item-label-cls} {
.@{form-item-cls} {
.@{form-item-label-cls}.@{form-item-required} {
&::before {
display: inline-block;
margin-top: 2px;
Expand Down
34 changes: 14 additions & 20 deletions docs/.vitepress/components/form/asyncValidator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,17 @@
/>
</FFormItem>
<FFormItem label=" ">
<FButton
type="primary"
style="margin-right: 20px"
:loading="modelForm.submitLoading"
@click="submitHandler"
>
{{ modelForm.submitText }}
</FButton>
<FButton
type="primary"
style="margin-right: 20px"
@click="clearHandler"
>
清除
</FButton>
<FButton type="primary" @click="resetHandler">重置</FButton>
<FSpace>
<FButton
type="primary"
:loading="modelForm.submitLoading"
@click="submitHandler"
>
{{ modelForm.submitText }}
</FButton>
<FButton type="primary" @click="clearHandler"> 清除 </FButton>
<FButton type="primary" @click="resetHandler">重置</FButton>
</FSpace>
</FFormItem>
</FForm>
</template>
Expand Down Expand Up @@ -147,14 +142,13 @@ export default {
try {
modelForm.submitLoading = true;
modelForm.submitText = '校验中';
const result = await formRef.value.validate();
await formRef.value.validate();
console.log(
'[form.validate] [submitHandler] 表单验证成功, result:',
result,
'[form.asyncValidator] [submitHandler] 表单验证成功',
);
} catch (error) {
console.log(
'[form.validate] [submitHandler] 表单验证失败, error:',
'[form.asyncValidator] [submitHandler] 表单验证失败, error:',
error,
);
FMessage.warn('请检查表单项');
Expand Down
54 changes: 24 additions & 30 deletions docs/.vitepress/components/form/complexValidate.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<FForm
ref="WFormDomRef"
ref="formRef"
labelWidth="140px"
labelPosition="right"
:model="modelForm"
Expand Down Expand Up @@ -92,17 +92,21 @@
:key="index"
style="display: flex; margin-left: 65px"
>
{{ `场景${index + 1}名称:` }}
<FFormItem :prop="`scenes[${index}].name`">
<FFormItem
:prop="`scenes[${index}].name`"
:label="`场景${index + 1}名称:`"
labelWidth="80px"
>
<FInput
v-model="modelForm.scenes[index].name"
:maxlength="10"
@input="validateScenesCont(index)"
/>
</FFormItem>
<span style="margin-left: 10px">内容:</span>
<FFormItem
:ref="(el) => (scenesDomRef[index] = el)"
:ref="(el) => (sceneFormItemRefList[index] = el)"
label="内容:"
labelWidth="80px"
:prop="`scenes[${index}].content`"
:rules="
modelForm.scenes[index].name
Expand All @@ -119,21 +123,11 @@
</div>

<FFormItem label=" ">
<FButton
type="primary"
style="margin-right: 20px"
@click="submitHandler"
>
提交
</FButton>
<FButton
type="primary"
style="margin-right: 20px"
@click="clearHandler"
>
清除
</FButton>
<FButton type="primary" @click="resetHandler">重置</FButton>
<FSpace>
<FButton type="primary" @click="submitHandler"> 提交 </FButton>
<FButton type="primary" @click="clearHandler"> 清除 </FButton>
<FButton type="primary" @click="resetHandler">重置</FButton>
</FSpace>
</FFormItem>
</FForm>
</template>
Expand All @@ -143,7 +137,7 @@ import { ref, reactive, computed } from 'vue';
export default {
setup() {
const WFormDomRef = ref(null);
const formRef = ref(null);
const rePasswordRef = ref(null);
const modelForm = reactive({
password: '',
Expand All @@ -156,7 +150,7 @@ export default {
{ name: '', content: '' },
],
});
const scenesDomRef = ref([]);
const sceneFormItemRefList = ref([]);
const validatePasswordStartWith = (rule, value) => {
return Boolean(
Expand Down Expand Up @@ -219,16 +213,16 @@ export default {
const validateScenesCont = (index) => {
// 通过 FFormItem 的动态 ref 调用 validate 校验
modelForm?.scenes[index]?.name
? scenesDomRef.value[index].validate()
: scenesDomRef.value[index].clearValidate();
? sceneFormItemRefList.value[index].validate()
: sceneFormItemRefList.value[index].clearValidate();
};
const submitHandler = async () => {
// 通过 FForm 的 validate 直接校验
try {
await WFormDomRef.value.validate();
await formRef.value.validate();
console.log(
'[form.complexValidate] [submitHandler] 表单验证成功~',
'[form.complexValidate] [submitHandler] 表单验证成功',
);
} catch (error) {
console.log(
Expand All @@ -238,18 +232,18 @@ export default {
}
};
const clearHandler = () => {
WFormDomRef.value.clearValidate();
formRef.value.clearValidate();
};
const resetHandler = () => {
WFormDomRef.value.resetFields();
formRef.value.resetFields();
};
return {
WFormDomRef,
formRef,
rePasswordRef,
modelForm,
rules,
scenesDomRef,
sceneFormItemRefList,
handlePasswordInput,
validateScenesCont,
Expand Down
6 changes: 6 additions & 0 deletions docs/.vitepress/components/form/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ complexValidate.vue
asyncValidator.vue
:::

### 嵌套验证

:::demo
nestValidator.vue
:::

## Form Props

| 属性 | 说明 | 类型 | 默认值 |
Expand Down
Loading

0 comments on commit 7827186

Please sign in to comment.