Skip to content

form.submit() to perform HTML5 validation #5139

@IlyaSemenov

Description

@IlyaSemenov

Package

v4.x

Description

As a developer working with forms, I rely not only to validation schemas, but also to HTML5 validation. Namely, I use <input required>, <input type="number" min= max=>, and I expect the form to display those errors natively. Most of the time it happens automatically when I use <button type="submit">.

The problem is, if I use u-form inside u-modal, the form goes to the modal body, and the submit button goes to the modal footer. As the result, the submit button can't natively perform form submit, and I have to do something like:

u-button(:disabled="form?.loading" @click="form?.submit") Submit

This works, but it doesn't trigger HTML5 validation.

What I propose is, in form.submit(), call the native validation before calling onSubmitWrapper:

async submit() {
await onSubmitWrapper(new Event('submit'))
},

The code could go like this instead:

async submit() { 
  const formEl = document.getElementById(formId)
  if (formEl?.reportValidity() !== false) {
    await onSubmitWrapper(new Event('submit'))
  }
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttriageAwaiting initial review and prioritizationv4#4488

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions