Skip to content

feat: allow multiple fields to be validated in one intent #878

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-needles-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@conform-to/dom': minor
---

Allow validating multiple fields in one validate intent
8 changes: 7 additions & 1 deletion packages/conform-dom/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,13 @@ function handleIntent<Error>(
switch (intent.type) {
case 'validate': {
if (intent.payload.name) {
meta.validated[intent.payload.name] = true;
if (typeof intent.payload.name === 'string') {
meta.validated[intent.payload.name] = true;
} else {
for (const name of intent.payload.name) {
meta.validated[name] = true;
}
}
} else {
setFieldsValidated(meta, fields);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/conform-dom/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export function replySubmission<FormError>(
export type ValidateIntent<Schema = any> = {
type: 'validate';
payload: {
name?: FieldName<Schema>;
name?: FieldName<Schema> | FieldName<Schema>[];
};
};

Expand Down
8 changes: 8 additions & 0 deletions playground/app/routes/form-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export default function FormControl() {
>
Validate Message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.validate.getButtonProps({
name: [fields.name.name, fields.message.name],
})}
>
Validate Name And Message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.update.getButtonProps({
Expand Down
16 changes: 16 additions & 0 deletions tests/integrations/form-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function getFieldset(form: Locator) {
number: form.locator('[name="number"]'),
validateForm: form.locator('button:text("Validate Form")'),
validateMessage: form.locator('button:text("Validate Message")'),
validateNameAndMessage: form.locator(
'button:text("Validate Name And Message")',
),
updateMessage: form.locator('button:text("Update message")'),
updateNumber: form.locator('button:text("Update number")'),
updateForm: form.locator('button:text("Update form")'),
Expand Down Expand Up @@ -57,6 +60,19 @@ async function runValidationScenario(page: Page) {
await expect(fieldset.message).toHaveValue('');
await expect(playground.error).toHaveText(['', '', '']);

await fieldset.validateNameAndMessage.click();
await expect(playground.error).toHaveText([
'Name is required',
'Message is required',
'',
]);

await fieldset.resetForm.click();
await expect(fieldset.name).toHaveValue('');
await expect(fieldset.number).toHaveValue('');
await expect(fieldset.message).toHaveValue('');
await expect(playground.error).toHaveText(['', '', '']);

await fieldset.updateForm.click();
await expect(fieldset.name).toHaveValue('Conform');
await expect(fieldset.number).toHaveValue('13579');
Expand Down
Loading