Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Form): add on-validate prop #5887

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

### NEXT_VERSION

### Features

- `n-form` adds `on-validate` prop, closes [#5883](https://github.com/tusen-ai/naive-ui/issues/5883).

## 2.41.0

### Breaking Changes
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

### NEXT_VERSION

### Features

- `n-form` 新增 `on-validate` 属性, 关闭 [#5883](https://github.com/tusen-ai/naive-ui/issues/5883)

## 2.41.0

`2025-01-05`
Expand Down
1 change: 1 addition & 0 deletions src/form/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ feedback-style.vue
| require-mark-placement | `'left' \| 'right' \| 'right-hanging'` | `'right'` | Require mark placement | `'right-hanging'` 2.24.0 |
| size | `'small' \| 'medium' \| 'large'` | `'medium'` | Size. | |
| validate-messages | `FormValidateMessages` | `undefined` | Validation messages of `async-validator`. | 2.27.0 |
| on-validate | `(path: string \| undefined, result: FormItemValidateResult) => void` | `-` | Callback executed after any form item is validated | NEXT_VERSION |

#### FormItemRule Type

Expand Down
12 changes: 10 additions & 2 deletions src/form/demos/enUS/partially-apply-rules.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ During the validation, you may not want to validate all items. You can use the s
</markdown>

<script lang="ts">
import type { FormInst } from 'naive-ui'
import type { FormInst, FormItemValidateResult } from 'naive-ui'
import { defineComponent, ref } from 'vue'

export default defineComponent({
Expand Down Expand Up @@ -34,6 +34,9 @@ export default defineComponent({
}
)
},
handleValidate(path: string | undefined, result: FormItemValidateResult) {
console.log('validate', path, result)
},
formInstRef,
model: ref({
fieldA: '',
Expand Down Expand Up @@ -70,7 +73,12 @@ export default defineComponent({
Restore validation
</n-button>
</n-space>
<n-form ref="formInstRef" :model="model" :rules="rules">
<n-form
ref="formInstRef"
:model="model"
:rules="rules"
@validate="handleValidate"
>
<n-form-item label="Min length 3" path="fieldA">
<n-input v-model:value="model.fieldA" />
</n-form-item>
Expand Down
1 change: 1 addition & 0 deletions src/form/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ feedback-style.vue
| require-mark-placement | `'left' \| 'right' \| 'right-hanging'` | `'right'` | 必填星号的位置 | `'right-hanging'` 2.24.0 |
| size | `'small' \| 'medium' \| 'large'` | `'medium'` | 尺寸 | |
| validate-messages | `FormValidateMessages` | `undefined` | `async-validator` 的默认验证信息 | 2.27.0 |
| on-validate | `(path: string \| undefined, result: FormItemValidateResult) => void` | `-` | 任一表项被校验后执行的回调 | NEXT_VERSION |

#### FormItemRule Type

Expand Down
12 changes: 10 additions & 2 deletions src/form/demos/zhCN/partially-apply-rules.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</markdown>

<script lang="ts">
import type { FormInst } from 'naive-ui'
import type { FormInst, FormItemValidateResult } from 'naive-ui'
import { defineComponent, ref } from 'vue'

export default defineComponent({
Expand Down Expand Up @@ -34,6 +34,9 @@ export default defineComponent({
}
)
},
handleValidate(path: string | undefined, result: FormItemValidateResult) {
console.log('validate', path, result)
},
formInstRef,
model: ref({
fieldA: '',
Expand Down Expand Up @@ -70,7 +73,12 @@ export default defineComponent({
清空验证
</n-button>
</n-space>
<n-form ref="formInstRef" :model="model" :rules="rules">
<n-form
ref="formInstRef"
:model="model"
:rules="rules"
@validate="handleValidate"
>
<n-form-item label="最短长度为 3" path="fieldA">
<n-input v-model:value="model.fieldA" />
</n-form-item>
Expand Down
1 change: 1 addition & 0 deletions src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type {
FormInst,
FormItemInst,
FormItemRule,
FormItemValidateResult,
FormRules,
FormValidationError
} from './src/interface'
9 changes: 6 additions & 3 deletions src/form/src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { FormTheme } from '../styles'
import type {
FormInst,
FormItemInst,
FormItemInternalValidateResult,
FormItemValidateResult,
FormRules,
FormValidateCallback,
FormValidateMessages,
Expand Down Expand Up @@ -62,7 +62,10 @@ export const formProps = {
type: Boolean as PropType<boolean | undefined>,
default: undefined
},
validateMessages: Object as PropType<Partial<FormValidateMessages>>
validateMessages: Object as PropType<Partial<FormValidateMessages>>,
onValidate: Function as PropType<
(path: string | undefined, result: FormItemValidateResult) => void
>
} as const

export type FormSetupProps = ExtractPropTypes<typeof formProps>
Expand Down Expand Up @@ -94,7 +97,7 @@ export default defineComponent({
return await new Promise<{ warnings: ValidateError[][] | undefined }>(
(resolve, reject) => {
const formItemValidationPromises: Array<
Promise<FormItemInternalValidateResult>
Promise<FormItemValidateResult>
> = []
for (const key of keysOf(formItems)) {
const formItemInstances = formItems[key]
Expand Down
9 changes: 6 additions & 3 deletions src/form/src/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { ExtractPublicPropTypes } from '../../_utils'
import type {
FormItemInst,
FormItemInternalValidate,
FormItemInternalValidateResult,
FormItemRule,
FormItemRuleValidator,
FormItemRuleValidatorParams,
FormItemValidateOptions,
FormItemValidateResult,
LabelAlign,
LabelPlacement,
ShouldRuleBeApplied,
Expand Down Expand Up @@ -262,7 +262,7 @@ export default defineComponent({
r => r.level === 'warning'
)

const validationResult: FormItemInternalValidateResult = {
const validationResult: FormItemValidateResult = {
valid: true,
errors: undefined,
warnings: undefined
Expand All @@ -277,7 +277,7 @@ export default defineComponent({
const warningValidator = new Schema({
[mergedPath]: activeWarningRules as RuleItem[]
})
const { validateMessages } = NForm?.props || {}
const { validateMessages, onValidate } = NForm?.props || {}
if (validateMessages) {
validator.messages(validateMessages)
warningValidator.messages(validateMessages)
Expand Down Expand Up @@ -339,6 +339,9 @@ export default defineComponent({
validationWarnedRef.value = !!validationResult.warnings
}

if (onValidate && activeRules.length > 0) {
onValidate(path, validationResult)
}
return validationResult
}
function handleContentBlur(): void {
Expand Down
4 changes: 2 additions & 2 deletions src/form/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface FormItemValidateOptions {
options?: ValidateOption
}

export interface FormItemInternalValidateResult {
export interface FormItemValidateResult {
valid: boolean
errors: ValidateError[] | undefined
warnings: ValidateError[] | undefined
Expand All @@ -54,7 +54,7 @@ export type FormItemInternalValidate = (
trigger: ValidationTrigger | string | null | undefined,
shouldRuleBeApplied?: ShouldRuleBeApplied,
options?: ValidateOption
) => Promise<FormItemInternalValidateResult>
) => Promise<FormItemValidateResult>

export type FormItemValidate = ((options: FormItemValidateOptions) => Promise<{
warnings: ValidateError[] | undefined
Expand Down