eastern-cyan
eastern-cyan4mo ago

how to set null or undefined to avoid setting -1 in edit form

how to set null or undefined to avoid setting -1 in edit form
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @yasuaki_jp. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Refine, you can set the default value of a form field to null or undefined using the useForm hook. However, there might be some type errors as the defaultValue type is BaseKey, which is string and number only, not null or undefined. You can try to bypass this by casting null or undefined as unknown first, then as undefined. Here's an example:
const { selectProps } = useSelect({
resource: "users",
optionLabel: "name",
optionValue: "id",
defaultValue: null as unknown as undefined,
});
const { selectProps } = useSelect({
resource: "users",
optionLabel: "name",
optionValue: "id",
defaultValue: null as unknown as undefined,
});
This might not work as expected, but it's worth a try. If you're trying to set a default value to null because of backend requirements, you might want to consider other solutions. For instance, if the default value of useSelect is an empty array [], you could send a null value if the array is empty. For more information, you can refer to the useForm documentation (https://refine.dev/docs/data/hooks/use-form/#properties) and the useSelect documentation (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-select#defaultvalue). Please note that these solutions are based on the information provided in the extracts and might not cover all possible scenarios or requirements. Always test your code thoroughly to ensure it works as expected. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@449197041306828802> kapa.ai is still learning and improving, please let us know how it did by reacting below