feat(useForm): Form state management and rule validation
See original GitHub issueClear and concise description of the problem
I want to develop a useForm
function to simplify form manipulation
Seek everyone’s opinion
Suggested solution
The current concept of the useForm
function
Code
<script lang="js" setup>
const { form, error, reset, onSubmit } = useForm({
// Initial form value
form: () => ({
age: '',
}),
// Verification rules
rule: () => ({
age: [
/* required */ val => !!val || t('required'), // i18n support
/* number */ val => !Number.isNaN(val) || 'Expected number',
/* length */ val => val.length < 3 || 'Length needs to be less than 3',
],
}),
})
function mySubmit() { /** Code */ }
</script>
<template>
<h3>Please enter your age</h3>
<form @submit.prevent="onSubmit(mySubmit)">
<label>
<input v-model="form.age" type="text">
<p>{{ error.age }}</p>
</label>
<button type="submit">
Submit
</button>
<button @click="reset">
reset
</button>
</form>
</template>
TODO
- args
- A function that returns the initial value of the form
- A function that returns the validation rules
- return
- func
- Manually trigger form validation
- reset form error
- Support clearing specified fields
- Submit (automatically verify rules before submitting)
- reset form data (restore default)
- reactive
- form object
- error message object
- whether the value is modified
- func
Alternative
No response
Additional context
Mind Mapping:
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:7 (6 by maintainers)
Top Results From Across the Web
useForm - FormState - Simple React forms validation
This object contains information about the entire form state. It helps you to keep on track with the user's interaction with your form...
Read more >useFormState - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >useForm | React Hook Form - Simple React forms validation
useForm is a custom hook for managing forms with ease. ... Rules. Schema validation focus on the field level for error reporting. Parent...
Read more >Advanced Usage - React Hook Form
React Hook Form has support for native form validation, which lets you validate inputs with your own rules. Since most of us have...
Read more >getFieldState - React Hook Form
Rules · name needs to match a registered field name. Copy · getFieldState works by subscribing to the form state update, and you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @userquin 👋. I’ve implemented the ability to use Yup for rules in SlimeForm, maybe you can try it~
This draft is the predecessor of SlimeForm and now I am continuing development in SlimeForm.
I mean, using it for the rules, for example
vee-validate v4
is using it, maybe for this is out of scope