Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
react hook forms is showing me incorrect (0) characters entered in text field and then correct field length on second click of submit

I am using getValues with react-hook-form and then trying to render the length of the characters of the text field in an error message when user goes over the limit. I am seeing 0/1046 characters when i click submit the first time then the second time I see the correct number.

const titleValue = getValues('title')
const descriptionValue = getValues('description')
  
   <TextField
                name='title'
                defaultValue=''
                label='Title*'
                control={control}
                css={{}}
                help={
                  errors.title?.type === 'required' || errors.title?.type === 'maxLength'
                    ? errors.title.message
                    : undefined
                }
                aria-label='title'
                rules={{
                  required: { value: true, message: 'Required' },
                  maxLength: {
                    value: 1024,
                    message: `${titleValue?.length || 0}/1024 maximum character length exceeded`,
                  },
                }}
              />

enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

react-hook-form provided an answer https://github.com/react-hook-form/react-hook-form/discussions/7668

  validate: (value) => {
    if (value.length >= 10) {
      // this will give you the correct value for your error message
      return `Please, enter a title with less than 10 characters (${value.length}/10)`;
    }
  }
}}```
about 4 years ago · Juan Pablo Isaza Report

0

Please refer to useForm - getValues

An optimized helper for reading form values. The difference between watch and getValues is that getValues will not trigger re-renders or subscribe to input changes.

Where as useForm - watch seems to fit your needs more.

This will watch specified inputs and return their values. It is useful for determining what to render.

const titleValue = watch('title')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!