TextField: onGetErrorMessage triggers on form load
- Package version(s): 0.82.1
For a TextField, is there a way to have onGetErrorMessage only trigger when onFocus or onBlur fire?
I don't think there is a straightforward way to do this. Sounds like a nice to have enhancement. Changes should be trivial. @joschect, @mikewheaton can you review e57d8be for this ?
@mdahamiwal: I can take a look. Can you submit a pull request for this?
Tenatively I think it looks fine, but I agree with @mikewheaton please submit a pr.
Thanks @joschect and @mikewheaton for taking a look. There are a few scenarios as part of this that are not very clear and require clarity:
- TextFields can have initial values that may not be valid. Should we validate upfront or defer validation until onFocus/onBlur fires? I think we should validate if the value exists.
- What happens when the component is re-rendered as part of prop changes? if we validate the initial value we should also validate prop changes even though onFocus/onBlur validation props are set.
I agree with you on both; we should run validation on the initial values and whenever it receives new props.
I am running into the same issue even after the fix for this issue has been merged in. It is not clear to me whether there is any way to disable the validation on page load? As far as I can tell, we are still calling this._validate(this.state.value); in componentWillMount() of the TextField component so from the looks of it, validation will always run when the component is instantiated.
@lszomoru, you concern may be similar to the one discussed in #770 . The workaround will work if the initial value is empty. However, If there is value on initial mount and you still want to suppress on Load validation, we will need to add another option for that.
Thanks @mdahamiwal. That is indeed the same issue that I am running into. I have two fields that are required, yet, as mentioned in issue #770 I would like to give the user the opportunity to fill in the fields before the validation errors occur. I do agree with the comment from #770 that there should be a way to suppress the validation on mount and maybe that should be the default behaviour.
I have read #770 and I am not sure I understand the workaround that you mentioned. How can I perform the basic required field validation without having the validation errors displayed on the form when the form loads? The only way to do that is to set the value to " " but that just seems broken to me. Thanks!
yes, the workaround is to return an empty errorMessage if value is empty but that might not work if user deletes all the text or there is some initial value. I have added an option to skip the on mount validation. Don't want to make it a default behavior since it may break current users. Hope that helps.
Thank you! Not changing the default behaviour makes sense. I will keep an eye on your pull request and will pick up the updated package when it is released. Thanks again for the quick turnaround on this issue.