Validation `values` 'undefined' with 'defaultValue' set
See original GitHub issueHello!
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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >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
@DamonBlais can we close this issue?
This can be closed.
Can’t find my 2fa for this
On Wed., Aug. 24, 2022, 5:41 p.m. Damon Blais, @.***> wrote: