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

How can i reset form,i can't found method clearValidationEffect method in form ref #96

Closed
nabaonan opened this issue Jun 13, 2021 · 7 comments

Comments

@nabaonan
Copy link

Environment Info

  • Naive UI version: 2.11.7
  • Vue version: 3.1.1
  • Browser Info: Chome 91.0.4472.77
  • System Info: macos 11.4

Reproduction link

https://codesandbox.io/s/naive-ui-test-vi95j?file=/src/App.vue:1326-1347

Steps to reproduce

click reset botton

What is expected?

the form can be reset

What is actually happening?

The form does not have a method called clearValidationEffect ,and with an error,but i found a method called restoreValidation,it is useful for clear validation ,but not for form's value

@07akioni
Copy link
Collaborator

07akioni commented Jun 13, 2021

The docs have mistakes, I've fixed it.

About resetting, naive-ui doesn't provide a method for reset a form. Form just collect values and do validation to the corresponding form items.

If you want to restore a value. Just implemenet it. For example:

{
  setup () {
    const formInitValue = { key: 'value' }
    const formValueRef = ref({ ...formInitValue })
    const resetFormValue = () => { formValueRef.value = formInitValue }
    // ...
  }
}

@jahnli
Copy link
Collaborator

jahnli commented Dec 1, 2021

@07akioni Writing this seems to lead to object double reference problems,

@jahnli
Copy link
Collaborator

jahnli commented Dec 1, 2021

@07akioni form.value = { ...formInitValue } 它是可以的

@07akioni
Copy link
Collaborator

07akioni commented Dec 2, 2021

@07akioni form.value = { ...formInitValue } 它是可以的

是的,我们这个表格由于实现的比较简陋,内部根本不去收集值,完全是受控的,想恢复就是记录一下初始值的事

@wangfpp
Copy link

wangfpp commented Feb 24, 2022

应该提供这个方法吧 还是经常使用的

@07akioni
Copy link
Collaborator

07akioni commented Feb 25, 2022

应该提供这个方法吧 还是经常使用的

提供不了,数据是用户自己控制的,表格改不了这玩意

@07akioni
Copy link
Collaborator

07akioni commented Feb 25, 2022

或许可以用 v-model:model 来做一个简单的清除工作,不过 emmm 确实感觉意义不大,省不了多少代码。

用户如果要自己搞这个事其实很简单,只多三行代码

const initialModel = { ... }

setup () {
  const modelRef = ref(deepClone(initialModel))
  return {
    model: modelRef,
    reset: () => modelRef.value = deepClone(initialModel)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants