fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

TextField: onGetErrorMessage triggers on form load

Open gabrielruss opened this issue 9 years ago • 10 comments

  • Package version(s): 0.82.1

For a TextField, is there a way to have onGetErrorMessage only trigger when onFocus or onBlur fire?

gabrielruss avatar Dec 13 '16 15:12 gabrielruss

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 avatar Dec 30 '16 09:12 mdahamiwal

@mdahamiwal: I can take a look. Can you submit a pull request for this?

mikewheaton avatar Dec 30 '16 17:12 mikewheaton

Tenatively I think it looks fine, but I agree with @mikewheaton please submit a pr.

joschect avatar Dec 30 '16 17:12 joschect

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:

  1. 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.
  2. 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.

mdahamiwal avatar Jan 04 '17 07:01 mdahamiwal

I agree with you on both; we should run validation on the initial values and whenever it receives new props.

mikewheaton avatar Jan 04 '17 17:01 mikewheaton

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 avatar Feb 20 '17 14:02 lszomoru

@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.

mdahamiwal avatar Feb 21 '17 05:02 mdahamiwal

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!

lszomoru avatar Feb 21 '17 10:02 lszomoru

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.

mdahamiwal avatar Feb 21 '17 10:02 mdahamiwal

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.

lszomoru avatar Feb 21 '17 11:02 lszomoru