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
Form always dirty? #577
Comments
Any chance you were rendering an input conditionally with a default value? Conform derives the dirty state simply by comparing the default value you provided with the latest form value. With v1.1, it also watch the doms on what is actually rendered to keep the form value in sync with the form. So if you got any input with a default value not rendered, it will consider dirty. |
I have hidden inputs, but surely they are ok? All controls used are Radix/UI, I will test removing these one at a time until I find if there any any custom control implementations that could be causing this |
OK that was it, I didn't include the 'intent' fields from my schema in the defaultValues. OK looks good now, just a few more rules to get this working 100%, however completely makes sense. |
This also lifted the dirty state on my form, and the solution does not seem that simple in my case. I think this change may create a lot of issues for this library in terms of being too strict and not allowing some additional fields that are not handled by Conform and should not be. There can be many reasons for having such fields and reasons for not wanting Conform to manage them or use them for its dirty state check or anything else for that matter. I tried an approach using the unstable useControl utility to handle some of the issues I had, but that feature is barely documented and it's not super clear to me how it works even though I read the PR comments. Maybe a helper utility that can be added as a prop on any (hidden) field which tells Conform to exclude this field from the comparison, etc., could be useful here? It also seems to me that including fields in (A bit later).... So after having wrote all this out and pondering a bit more without sending it, it seems using |
@houmark I also had the same issue where I had in my objects dates and other info that I wanted for other controls on the form. And, like you had to remove these from my object before passing into Conform, this was a pain for sure. I like the idea that the schema should be the source of truth as to which fields are checked as part of the defaultValue. As this is what we are checking against for form validation. What do you think @edmundhung ? |
Yeah, I cannot think of any downside with Conform parsing the It's hardish to debug or even understand why the form became dirty. I had to debug out Anyways, just my two cents — and I have now gotten my form to play 99% good with Conform and clean/dirty state. For what it's worth, here's my debugging code for checking how Conform sees in the form, it should match the form fields you have (including any hidden fields) — and keep an eye on when it just loads the form as it can quickly shift after the first render(s): <pre>
{JSON.stringify(form.value)}
</pre> |
@houmark cheers for the debug code, I might use this in some more complicated forms. I must admit though that 1.1.0 has fixed my dirty form issues I had with tabbing through fields, and I love that I can make a change in a field, then revert the changes and the form dirty state goes back to false.... exactly how I want it to work! cheers @edmundhung for another great update! |
Yes, and I'm sorry if that wasn't clear in my prior comments. I am a fan of this change in the end, but it was a bit tough to get to the happy place and I can see others struggling with getting there when first using Conform or when updating, especially with the current documentation. That's why I'm suggesting either better internal handling to remove most of the potential blockers/hold ups and/or add automatic console logging or helper functions for debugging, so it becomes easier to visualize what is holding up a happy Conform. The 1% that has not worked for me in my lightly advanced form is resetting which I cannot get to work fully for all fields (some are using |
This won't work reliable because there is no way to assume that you have a constraint for every field. It also wasn't a required option so it won't work for other cases as well.
Conform never maintains the form value. It syncs the value with the FormData API so anything get rendered in the form will be reflected on There are two solutions right now:
function Component() {
const [form, fields] = useForm({
shouldDirtyConsider(name) {
// e.g. ignore intent and CSRF token that are not mangaged by Conform
return name !== 'intent' || name !== 'csrf_token';
},
});
// ...
} |
The shouldDirtyConsider is perfect, thanks, I hadn't spotted this. Is there a way to see which fields are considered dirty? I have some quite complex forms with many nested objects and arrays, would be good if there was a way to tell which property has caused the for to be dirty. At present it is quite the task to check a complex form for the offending field/control. Especially if I have a component library with custom components. |
Actually is appears that the shouldDirtyConsider doesn't process nested objects, I have a console output and it isn't processing each property of an object, is this correct? I have also noticed that the shouldDirtyConsider looks at the form controls, but the issue we have is when there are extra fields in the defaultValue... hmm, not an easy one to sort out, especially with complex forms. |
Describe the bug and the expected behavior
After installing 1.1.0 it appears that form.dirty is always true. I have Save Buttons disabled unless there have been changes, these are always enabled now, they are controlled disabled={!form.dirty}
Conform version
c1.1.0
Steps to Reproduce the Bug or Issue
disabled={!form.dirty}
What browsers are you seeing the problem on?
No response
Screenshots or Videos
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: