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
Bug: manipulating controlled input value breaks native validation #27621
Comments
In the example you provided, you are using React to create a text input component that converts the entered text to uppercase. However, you have noticed that this can interfere with the native HTML validation, especially when the input does not meet validation requirements, as in the case of "a". This happens because the actual value of the input is not altered; only the value displayed in the user interface is changed. To work around this issue, you can use the onInput event instead of the onChange event. The onInput event is triggered whenever the value of the input field is modified, whereas the onChange event may be triggered after the input field loses focus, which is not ideal for real-time validation. |
@felipe-rodolfo React's Also if |
You are correct, and I apologize for any confusion. React's documentation does indicate that In most cases, using To address the validation issue you initially described, you may need to adjust how you handle transformations in the to solve the validation issue when converting text to uppercase while still using the onChange event, you can follow this approach: Keep using the onChange event to update the component state and convert the text to uppercase. Add a custom validation function that checks if the value meets the requirements, such as the minimum length, within the onChange event. Use setCustomValidity to set up custom validation based on the established rules. Here's a modified code example:
|
Could you please not clutter the issue with chatGPT responses? |
Dunno why but I haven't bothered checking if it works the same way with vanilla JS and HTML - and it has the same error |
If you have a controlled input with some restrictions(like minLength for example) manipulating input value in any way in onChange handler breaks native validation.
For example if you have
onChange={e => setText(e.target.value.toUpperCase())}}
this will break if your value after applying uppercase is different than the initial target value. So an input like "123" works but "a" doesn't - in the latter example HTML will consider input value to be valid.React version:
18.0.0
Steps To Reproduce
You can check-out code example below which showcases the issue - if you input any letter the background will turn green immediately indicating the the value is valid even though it doesn't meet minLengh requirement. If you input a number instead it works as intended(because
"1"
is the same as"1".toUpperCase()
)Link to code example:
https://playcode.io/1645770
The current behavior
Explained above
The expected behavior
It should handle validation correctly as with regular input
The text was updated successfully, but these errors were encountered: