-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
breaking: remove submitter option from experimental form validate(…) method, always provide default submitter
#14762
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
base: main
Are you sure you want to change the base?
Conversation
…)` method, always provide default submitter
🦋 Changeset detectedLatest commit: 8a67bb9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
I can see a case where the validity of a field could depend on which button you press, so I would vote for keeping it as an option (especially since it's already semi hidden inside an object parameter). I do think that defaulting to the first button / submit input is great though. |
|
'can see a case' as in you've encountered it/consider it a realistic outcome, or more of a hypothetical? All API surface area has a cost, so if it's the latter then I still vote for removal. I'm not even sure how you'd use such an option in practice. |
|
I guess it's such a small likelyhood that it probably wouldn't come up realistically? The closest thing I've built is a media control form that has play/pause buttons as submitters, where you would have a case like "play is only valid if the current state is paused". |
|
I agree with Ottomated. I talked about a realistic situation with my boss before where two buttons are needed and it’s when you have a “save as draft” and “publish” button. This is pretty common for our clients. It’s highly likely the form is always valid if you click the draft button but is stricter when clicking publish. I don’t know if you’d want different remote functions for the two actions when you can just slightly differ the logic depending on the submitter value. Maybe it’s better to just pass the value of the button directly? This might make sense since we’re already manually passing in the submitter. Slightly more maintenance required if the button value changes but it’s less boilerplate. I agree the current API is clunky but I couldn’t think of a better way. |
|
How would you use it though? The form is either valid or invalid, and it's a state the form is in rather than a return value of |
How the button value is useful in validation schemasPersonally, I've used Valibot's When programmatic validation would be usedIf the main use case for programmatic validation is the "validate on input" feature, we probably also want this when there are "draft" and "publish" buttons. In this case, we most likely want it to "validate on input" using the draft validation schema which is less strict. To achieve that using a union of schemas, we would need a way to pass in the "draft" value for that submitter field and not the default submitter's value so that it knows it should use the draft validation schema. |
|
Another thing you could do, if making the 'publish as draft' button the default isn't possible: do the refinement inside the handler, using the That's probably less nice than using |
#14475 added a
submitteroption tomyform.validate(...), but I'm not convinced it's the right move. Aside from feeling a bit clunky (you have to have alet submitterin your<script>together with abind:this, it feels weird to say that a form's validity is dependent on which button the user ends up pressing, especially when we can't know that until they press it. While it is possible to construct a schema that's valid for one submitter but not another, it's very much not the norm: fields are typically valid or invalid in and of themselves.And unlike
<input>elements whose values change according to user input,<button>values are set by the developer. If they're invalid, it's a developer error rather than a user error.Instead, I think we missed a trick: we should just include the default submitter (which, per WHATWG, is the first
<button>inside the<form>) when constructing theFormDataobject.The alternative would be to do both (include the default submitter and have an option for overriding it) but I really think that's just unnecessary API clutter.
cc @ottomated since you opened #14475
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits