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.

Validation `values` 'undefined' with 'defaultValue' set

See original GitHub issue

Hello!

So, I have a lengthy, multi-page form that I’ve used LocalStorage to save the state therein.

const [state, setState] = useState<AccountSetupForm>({} as AccountSetupForm)

// Load from LocalStorage on Page Load
useEffect(() => {
  const savedState = window.localStorage.getItem(LSKEY_ACCOUNT_SETUP_FORM)
  if (savedState) {
    setState({ ...state, ...JSON.parse(savedState) })
  }
}, [])

However, when I restore the state to the form (which updates its values), the validation doesn’t see this.

The Next/Submit button in the form is disabled on load, even if all fields were populated on load.

const [disabled, setDisabled] = useState<boolean>(false)
return (
  <Form
    onValid={() => setDisabled(false)}
    onInvalid={() => setDisabled(true)}
  >
    {/* ... */}
  </Form>
)

Then, upon editing any field, only that field is defined in the validation, all else are undefined.

addValidationRule('isPhone', (values: Values, value?: string): boolean => {
  console.log(values) // only the field I edited is defined, everything else is `undefined`
  setState(values) // is there a better way to access the form state than inside a validator?

  if (value) {
    const code = values['phone-country-code']
    if (code) { // <-- `code` is `undefined` even though its field has a `defaultValue`
      return validator.isMobilePhone(
        `${code.slice(0, code.indexOf(':'))}${value}`,
        'en-US',
        {
          strictMode: true,
        }
      )
    }
  }

  return false
})

How do I tell the validation to grab the values of all the fields after I’ve loaded them?


Example Form Input (for completeness of reproducability sake):

<Form.Input
  autoComplete="email"
  defaultValue={state.Email}
  label="Email Address"
  name="email"
  type="email"
  
  {/* Had to wrap this because idk how to access values except in a validator */}
  validations="isEmailWrapped"

  validationErrors={{
    isEmail: 'Doesn\'t look like a valid email address...',
  }}
  errorLabel={errorLabel}
/>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
felixmoshcommented, Aug 24, 2022

@DamonBlais can we close this issue?

0reactions
DamonBlaiscommented, Aug 25, 2022

This can be closed.

Can’t find my 2fa for this

On Wed., Aug. 24, 2022, 5:41 p.m. Damon Blais, @.***> wrote:

Sorry, that’s a GitHub account I don’t use often (generally by mistake.)

I’ll try and login to close this.

— Reply to this email directly, view it on GitHub https://github.com/formsy/formsy-react/issues/642#issuecomment-1226639191, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKTKHMIF7CR2KA2HG2E7CQDV226LNANCNFSM47LZESAA . You are receiving this because you were mentioned.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I have undefined defaultValue with react-hook-form?
But...the thing is. The value needs to be undefined. The user needs to select a value and otherwise it will get a validation...
Read more >
[Validation] Control with default value · Issue #702 - GitHub
I have a Form with one Control with default value. ... On Load, isRequired Validation value always return undefined.
Read more >
useForm | React Hook Form - Simple React forms validation
The defaultValues prop is used to populate the entire form values. It supports both sync and async set form default values. You can...
Read more >
Typing React Context to avoid an undefined default value
The main problem caused by this approach is we will have to check for undefined every time we will try to consume our...
Read more >
Input Components - React-admin - Marmelab
React-admin provides a set of Input components, each one designed for ... Validator functions should return undefined if the value is valid, ...
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