question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(useForm): Form state management and rule validation

See original GitHub issue

Clear 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

Alternative

No response

Additional context

Mind Mapping:

image

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
LittleSoundcommented, Jun 4, 2022

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.

1reaction
userquincommented, May 13, 2022

@LittleSound have you check yup?

I didn’t know yup before, what do you want to say?

I mean, using it for the rules, for example vee-validate v4 is using it, maybe for this is out of scope

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found