-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
preserved validation results/errors for array items #508
Comments
Related question: Once the user tries to submit a form, all fields are validated. In case where the submission fails due to validation errors, the user could make a change afterwards that reveals more form fields (perhaps by changing a "kind" attribute which controls a discriminating union in the zod schema). One could argue that the newly revealed fields should immediately be validated to be consistent with the fields that were already there before the attempted submit. Or, one could argue that they should not be validated until the user actually focussed->inputted->blurred something on the new field. Does Superforms have an opinion on this? |
@linus-amg since your If you want to keep const deleteItem = (index) => () => {
$formData.tags = $formData.tags.filter((_, i) => i !== index);
validateForm({update: true});
}; |
@pekeler Good question, I have no opinion really but I think the default focus-input-blur is fine. If you have any further ideas about it, please let me know! |
@ciscoheat I've tried your recommendation of using validateForm, but I guess I was not yet able to bring across the actual issue, the problem is not that it's not validating, the problem is that it is validating, even though I'm not submitting in between deletes and adds, but still get errors for items at an index which was invalid before. I created two more recordings trying showcase the issues, one could also try it out in the SvelteLab link, I've updated the code to use "auto", but it's the same behavior. Screen.Recording.2024-11-13.at.09.56.43.movScreen.Recording.2024-11-13.at.09.57.12.mov |
I see, then you need to use const addItem = (evt) => {
evt.preventDefault();
formData.update($formData => {
$formData.tags = [...$formData.tags, { timestamp: Date.now() }];
return $formData;
}, {taint: false});
};
const deleteItem = (index) => () => {
formData.update($formData => {
$formData.tags = $formData.tags.filter((_, i) => i !== index);
//validate(`tags[${index}]`);
return $formData;
}, {taint: false})
}; |
Thank you very much for the suggestion @ciscoheat - will give it a try. |
Screen.Recording.2024-11-11.at.16.40.08.mov
Description
Validation results seem to stay in some kind of cache when an invalid element gets deleted from an array, while the expectation is that the errors of this item would also get reset.
MRE: https://www.sveltelab.dev/nl0wj3phemks2d5
Instructions:
add
button to add a new item to an arraysubmit
button to trigger client-side validationdelete
button to delete the item from the arrayadd
buttonadd
one more timeSame results with trying all other
validationMethod
.The text was updated successfully, but these errors were encountered: