material-ui: [Autocomplete] The value provided to Autocomplete is invalid. (with the installed isOptionEqualToValue)
Duplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
I get an «value provided to Autocomplete is invalid» error:
None of the options match with `{"id":18877,"YearFrom":2002,"YearTo":2005,"Make":"Hyundai","Model":"Getz","Generation":"I (2002—2005)","FuelType":"Бензин","DriveType":"Передний","Transmission":"Автомат","Modification":"1.4 AT (97 л.с.)","Power":97,"EngineSize":"1.4","BodyType":"Хетчбэк","Doors":"5"}`.
You can use the `isOptionEqualToValue` prop to customize the equality test.
Expected behavior 🤔
Well, if i use the isOptionEqualToValue={(option, value) => option.id === value.id}, the expected behavior is that I shouldn’t get an error.
Steps to reproduce 🕹
Steps:
- Use Autocomplete:
<Autocomplete
multiple
fullWidth
filterSelectedOptions
defaultValue={defaultValues}
options={options}
isOptionEqualToValue={(option, value) => option.id === value.id}
getOptionLabel={(option: ICar) =>
`(${option.id})
${option.Make} ${option.Model} ${option.Generation}
${option.EngineSize} л ${option.FuelType?.toLowerCase()} ${option.Power} л.с. – ${option.Transmission?.toLowerCase()} ${option.DriveType?.toLowerCase()} привод
`
}
renderInput={(params: AutocompleteRenderInputParams) => <TextField {...params} label={'Применяемость'} />}
renderOption={(props, option: ICar) => {
return (
<li {...props}>
{option.Make} {option.Model} {option.Generation} {option.EngineSize} л {option.FuelType?.toLowerCase()} {option.Power} л.с. – {option.Transmission?.toLowerCase()} {option.DriveType?.toLowerCase()} привод
</li>
)
}}
onChange={(_, value) => onChange(value)}
onInputChange={(event, value) => {
if (value && value.length >= 3) {
search(value).finally()
}
}}
/>
- The defaultValue is also a type Array<ICar>
- I get an error with defaulValue options only
Context 🔦
No response
Your environment 🌎
`npx @mui/envinfo`
System:
OS: macOS 12.0.1
Binaries:
Node: 16.9.1 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.6 - ~/node_modules/.bin/npm
Browsers:
Chrome: 96.0.4664.45
Edge: Not Found
Firefox: 94.0.1
Safari: 15.1
npmPackages:
@emotion/react: ^11.4.1 => 11.4.1
@emotion/styled: ^11.3.0 => 11.3.0
@mui/core: 5.0.0-alpha.50
@mui/icons-material: ^5.0.3 => 5.0.3
@mui/material: ^5.0.3 => 5.0.3
@mui/private-theming: 5.0.1
@mui/styled-engine: 5.0.1
@mui/styles: 5.0.1
@mui/system: 5.0.3
@mui/types: 7.0.0
@mui/utils: 5.0.1
@mui/x-data-grid: ^5.0.0-beta.3 => 5.0.0-beta.3
@types/react: ^17.0.20 => 17.0.20
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
typescript: ^4.4.3 => 4.4.3
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 13
- Comments: 36 (8 by maintainers)
Could we please get an option to turn off the warning completely? At this stage it’s causing a lot of noise in tests and, as reported, it’s not warning anything useful.
@michaldudak I added PR with
disableOptionEqualToValueWarningprop which disables that warning. I would appreciate any suggestions if you guys have any ideas about naming. ThefreeSoloattribute modifies the standard behavior ofAutocompletecomponent that’s why it was not a solution in my caseApparently, at this point, the warning causes more confusion than assistance. If anyone is willing to investigate if this warning can be disabled for cases of async loading without much hassle, then feel free to grab this issue.
I’m also considering removing the warning altogether, but let’s try the less destructive option first.
I have multi select
AutoComplete. This is the way I solve the problem:freeSolo,autoSelectto ignore warningclearOnBlurto ignore value that not [Enter] or throw selecting an option in dropdownonChangeto handle unwanted textHappy coding 😃
In case anyone stumbles upon this issue today still, the trick for async-populated options is to add the currently selected option so that it doesn’t trigger the false positive that can happen when setting a new value while an async request changes the array of options that is provided.
Hi all. Is there an update on this issue? I agree 200% with @derekcannon , as I am using multi-select autocomplete with queried options upon keyboard input. So the selected options may not be on the current options, as the options are queried upon input and dynamic. Everything works fine, but I would love to suppress the Warning…
disableOptionEqualToValueWarning<-- looks sweet to me! Thanks! @grzegorz-jazurekI have the same Issue … boring warning
I have this issue as well, and I think its the same case. It can happen when implementing an external async search. The Autocomplete component still has a value, but the options are now populated with the results from the search, which does not include the current value.
Here is a reproduction: https://codesandbox.io/s/asynchronous-material-demo-forked-q0yox?file=/demo.tsx
Steps to repro:
It doesn’t repro 100% of the time, but its pretty easy to get it to happen.
I have got the same issue - getting the warning: “The value provided to Autocomplete is invalid. None of the options match with “”. You can use the
isOptionEqualToValueprop to customize the equality test”.Adding the final part
|| value === ""toisOptionEqualToValue={(option, value) => option.name === value.name || value === ""removes the warning, but then a new problem appears: all options turn colored as if all were chosenThanks for the reproduction, @markedwards. I’m reopening the issue.
I can’t say I like the idea of introducing yet another prop to the Autocomplete, especially just for suppressing a warning. @DiegoAndai, @mj12albert, @mnajdova, I propose to remove the warning altogether.
For anyone who may still be running into this warning. The issue I ran into was a mislabelling of “freeSolo”, with a capital “S”.
still an issue for me. not wanting to add more state as said in @angrybacon answer, because i use react hook form mainly to control the state.
can i just return true for
isOptionEqualToValueprop? is it affect anything., didn’t find the effect so far. even still don’t get it the purpose of that prop in the first place@mnajdova given that this issue is getting activity consistently, I think we should remove the warning for v6. What do you think?
I had a bit different kind of a problem that is described in this thread, but as it lands on same field I decided to post here rather to post new thread (hope that’s fine).
TL;DR
A opinion that has very minor affect on… anything. May resolve some common issues found in internet with this component.
Decription
I have MUIv5 and I use MUI Autocomplete, with Formik (with Yup) and use typescript. I’ve come into an opinion where I feel like that useAutocomplete (
@mui/base/AutocompleteUnstyled/useAutocomplete.js) applies bad practices.By the snipped code below, it states that the Autocomplete initial value can not be
undefined, but rathernull? – henche, to my knowledgeundefinedis stated as “not set” or in validation schemes “not touched” andnullis actually a defined value ("someone or something have changed or set it’s value to null).In my case this caused the form to be initialized with “undefined”… string (what the …) and bunch of error/warning messages, saying:
Material-UI: the value provided to Autocomplete is invalid. None of the options match with ´""´.The getOptionLabel method of Autocomplete returned undefined instead of a string for "".Curtain call
I resolved my problem by initializing form values with null and applying
nullableinto Yup validation schema for the fields.Sample code:
Any updates on this? Looks like this suggestion could allow fixing the issue.
I forked the controlled input example from the documentation and tweaked it slightly, working towards what I was actually building (albeit not for Harry Potter characters).
Although the warning does not show up, there is a lot of questionable UX going on. You will notice the behavior where you delete half the name and it reappears on blur because of the odd implementation choice of having the
Autocompletehave two values.I’ve “solved” that by setting
valuevia theonInputChangehandler, which removes the need for two pieces of state, and removes the weird ambiguity between the two competing/conflicting values. But the documentation explicitly advises against this.Of course, creating this dummy value object does make it clear why the warning shows up.
Which would bring me back to: Either the Autocomplete component should be completely re-thought (probably the “right” solution) w.r.t. multiple values and setters, OR the warning should be ditched and you can set the value to whatever you want.
The latter also does feel like a “right” choice considering entering non-specific options is kind of the whole thing that sets
<Autocomplete />apart from<Select />. If you don’t want the user to be able to type in random other text, use a<Select />, no?