Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextInput validation not triggering during typing(onChange). #9547

Open
david-hov opened this issue Dec 26, 2023 · 5 comments
Open

TextInput validation not triggering during typing(onChange). #9547

david-hov opened this issue Dec 26, 2023 · 5 comments
Labels

Comments

@david-hov
Copy link

david-hov commented Dec 26, 2023

I tried all modes and revalidate values

What you were expecting:
e.g. Validate first name, when inserted dash immediatly show error message.

What happened instead:
only when onBlur, even when I put onChange mode or all, it only triggers when onBlur, but need real time validation to show missing or wrong elements to user.

Steps to reproduce:

Related code:

<SimpleForm
      onSubmit={handleSubmit}
      className='sign-up-form'
      validate={(values) =>
          validateSignUpFirstStep(values,
              timerId,
              setEmailChecked,
              setInputDisabled,
              email,
              setTimerId)
      }
      mode='all'
      reValidateMode='onChange'
      style={{ padding: '0' }}
      toolbar={<CustomToolbar />}
>
          <div className='names-fields'>
              <TextInput className='first-name' variant='outlined' validate={required()} fullWidth source='firstName' label='First Name' />
              <TextInput variant='outlined' validate={required()} fullWidth source='lastName' label='Last Name' />
        </div>
</SimpleForm>

Other information:
https://codesandbox.io/p/devbox/validationtest-djclwn

Go to Posts and create, need to trigger during typing validation but only trigger when onblur

Environment

  • React-admin version: 4.0.0
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18.1.0
  • Browser: Chrome
  • Stack trace (in case of a JS error):
@david-hov david-hov changed the title TextInput validation not triggering during that input typing. TextInput validation not triggering during typing(onChange). Dec 27, 2023
@david-hov
Copy link
Author

david-hov commented Dec 27, 2023

But when blur and again focus it's starting working.
Now for temporary solution for me was to set on every TextInput
onChange={(e: any) => {
e.target.blur();
e.target.focus();
}
to show error runtime.
but mode='onChange' or mode='all' should handle that.

@david-hov
Copy link
Author

david-hov commented Dec 28, 2023

As I can not see any answeres, I created seperate function which I call like this
<TextInput onInput={validateOnType} source='email' validate={[required('This field is required'), validateInput]}/>
---and validateOnType function like this---

export const validateOnType = (e: any) => {
    // react-hook-form issue, temporary workaround
    if (e.target.value.length === 1) {
        e.target.blur();
        e.target.focus();
    }
}

which only triggered when first letter/symbol/number... entered.

@fzaninotto
Copy link
Member

Confirmed, thanks for the report.

@fzaninotto fzaninotto added the bug label Jan 3, 2024
@djhi
Copy link
Contributor

djhi commented Jan 19, 2024

Isn't it because you have both form level and input level validation? react-hook-form does not support both on the same form, as explained in our documentation (https://marmelab.com/react-admin/Validation.html):

https://marmelab.com/react-admin/Validation.html

@djhi djhi added needs more info and removed bug labels Jan 19, 2024
@david-hov
Copy link
Author

david-hov commented Jan 20, 2024

Isn't it because you have both form level and input level validation? react-hook-form does not support both on the same form, as explained in our documentation (https://marmelab.com/react-admin/Validation.html):

https://marmelab.com/react-admin/Validation.html

no as you can see I am usin only global validation , you can see in sandbox
as documentation says
{/*
We need to add validate={required()} on required fields to append a '*' symbol
to the label, but the real validation still happens in validateUserCreation
*/}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants