Class NativeForm

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.HtmlContainer
com.vaadin.flow.component.html.NativeForm
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasText, Serializable

@NullMarked @Tag("form") public class NativeForm extends com.vaadin.flow.component.HtmlContainer
Component representing a <form> element.

A native form is submitted by the browser itself: when the user activates a submit button inside it, the browser sends the values of the named fields (Input.setName(String)) to the action URL with the given method and navigates to the response. The values do not go through the server-side component state, so use this for posting to an endpoint outside of Vaadin, such as the login processing URL of Spring Security, rather than for editing data with a Binder.

The browser submits only native form controls, such as <input>, and form-associated custom elements with a form, so use Input for the fields and NativeButton for the submit button. Vaadin field components are not form-associated custom elements: depending on the component they submit nothing, a formatted or internal value instead of the field value, or only work by chance because of their internal markup, and a Vaadin Button does not submit the form. None of this is reported as an error.

Submitting the form makes the browser leave the current page, unless a target sends the response to another window or frame. The state of the Vaadin UI on the page is then lost. Keep in mind what triggers a submit:

  • A NativeButton submits the form unless its type is set to NativeButton.Type.BUTTON, because submit is the HTML default. Set that type on every button in the form that should only run its click listeners.
  • Pressing Enter in an Input submits the form, even when the form has no submit button but only a single text field.
  • A click listener on a submit button races with the navigation: the browser may leave the page before the server has handled the click, so do not rely on it.

The form is submitted with NativeForm.Method.POST unless another method is set, so that field values such as passwords do not end up in the URL, the browser history and server logs. A POST to an endpoint protected by Spring Security needs a CSRF token as a hidden Input named after the token parameter. The login processing URL of a view configured with VaadinSecurityConfigurer#loginView is exempt from this.

 Input username = new Input();
 username.setName("username");
 username.setAutocomplete("username");
 username.setRequiredIndicatorVisible(true);
 Input password = new Input();
 password.setType("password");
 password.setName("password");
 password.setAutocomplete("current-password");
 password.setRequiredIndicatorVisible(true);
 // Keep the password out of the Vaadin session, see below
 password.setValueChangeMode(null);
 NativeButton submit = new NativeButton("Log in");
 submit.setType(NativeButton.Type.SUBMIT);

 NativeForm form = new NativeForm(username, password, submit);
 form.setAction("login");
 

HasValueAndElement.setRequiredIndicatorVisible(boolean) sets the native required flag, so the browser does not submit the form while such an input is empty, and Input.setAutocomplete(String) helps password managers fill in a login form.

An Input sends its value to the server on every change by default, so the value is also kept in the component state of the Vaadin session, which may be serialized. Call setValueChangeMode(null) on an input for a password or another secret that only the action URL should receive.

Forms can not be nested in HTML; the browser drops the inner <form> tag. Adding a NativeForm directly to another NativeForm therefore throws an IllegalArgumentException.

Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The encoding of the form data when the form is submitted with NativeForm.Method.POST.
    static enum 
    The HTTP method the browser uses to submit the form.

    Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText

    com.vaadin.flow.component.HasText.WhiteSpace
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty form that is submitted with NativeForm.Method.POST.
    NativeForm(com.vaadin.flow.component.Component... components)
    Creates a new form with the given children that is submitted with NativeForm.Method.POST.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Collection<com.vaadin.flow.component.Component> components)
    void
    addComponentAtIndex(int index, com.vaadin.flow.component.Component component)
    <V extends @Nullable Object, S extends com.vaadin.flow.signals.Signal<V>>
    void
    bindChildren(com.vaadin.flow.signals.Signal<List<S>> list, com.vaadin.flow.function.SerializableFunction<S,com.vaadin.flow.component.Component> childFactory)
    @Nullable String
    Gets the URL the browser submits the form to.
    Gets how the browser encodes the form data when submitting it.
    Gets the HTTP method the browser uses to submit the form.
    Gets the window, tab or frame that shows the response when the form is submitted.
    void
    replace(@Nullable com.vaadin.flow.component.Component oldComponent, @Nullable com.vaadin.flow.component.Component newComponent)
    void
    setAction(@Nullable String action)
    Sets the URL the browser submits the form to.
    void
    setEnctype(@Nullable NativeForm.Enctype enctype)
    Sets how the browser encodes the form data when submitting it with NativeForm.Method.POST.
    void
    setMethod(@Nullable NativeForm.Method method)
    Sets the HTTP method the browser uses to submit the form.
    void
    setTarget(@Nullable AnchorTargetValue target)
    Sets the window, tab or frame that shows the response when the form is submitted.
    void
    setUnsafeAction(@Nullable String action)
    Sets the URL the browser submits the form to without validating its scheme.

    Methods inherited from class com.vaadin.flow.component.HtmlComponent

    getTitle, setTitle

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, bindVisible, findAncestor, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, getUIOrThrow, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttached

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasComponents

    add

    Methods inherited from interface com.vaadin.flow.component.HasComponentsOfType

    add, addComponentAsFirst, getChildren, getComponentAt, getComponentCount, indexOf, remove, remove, removeAll

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    bindEnabled, isEnabled, setEnabled

    Methods inherited from interface com.vaadin.flow.component.HasSize

    bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName

    Methods inherited from interface com.vaadin.flow.component.HasText

    bindText, getText, getWhiteSpace, setText, setWhiteSpace
  • Constructor Details

    • NativeForm

      public NativeForm()
      Creates a new empty form that is submitted with NativeForm.Method.POST.
    • NativeForm

      public NativeForm(com.vaadin.flow.component.Component... components)
      Creates a new form with the given children that is submitted with NativeForm.Method.POST.
      Parameters:
      components - the children components
      Throws:
      IllegalArgumentException - if one of the components is a NativeForm
  • Method Details

    • setAction

      public void setAction(@Nullable String action)
      Sets the URL the browser submits the form to. A relative URL is resolved against the <base href> that Vaadin sets to the root of the application, so "login" targets the login path of the application, while "/login" ignores its context path.
      Parameters:
      action - the URL to submit to, or null to remove the attribute, in which case the browser submits to the URL of the current page
      Throws:
      IllegalArgumentException - if the URL uses a scheme that is not considered safe, such as javascript:. The "safeUrlSchemes" configuration is read from the application that this form is attached to, so for a form that isn't attached yet the exception is instead thrown when it is attached. See setUnsafeAction(String) for setting a URL that shouldn't be checked at all.
    • setUnsafeAction

      public void setUnsafeAction(@Nullable String action)
      Sets the URL the browser submits the form to without validating its scheme.

      Unlike setAction(String), this method does not reject URLs based on the "safeUrlSchemes" configuration. Use it only for URLs that are fully under your control and known to be safe. Passing untrusted input here can expose the application to cross-site scripting (XSS) attacks, because a javascript: action runs when the form is submitted.

      Parameters:
      action - the URL to submit to, or null to remove the attribute
      See Also:
    • getAction

      public @Nullable String getAction()
      Gets the URL the browser submits the form to.
      Returns:
      the action URL, or null if not set
      See Also:
    • setMethod

      public void setMethod(@Nullable NativeForm.Method method)
      Sets the HTTP method the browser uses to submit the form. A new form uses NativeForm.Method.POST.
      Parameters:
      method - the method, or null to remove the attribute, in which case the browser uses NativeForm.Method.GET
    • getMethod

      public @Nullable NativeForm.Method getMethod()
      Gets the HTTP method the browser uses to submit the form.
      Returns:
      the method, or null if not set or not recognized
      See Also:
    • setEnctype

      public void setEnctype(@Nullable NativeForm.Enctype enctype)
      Sets how the browser encodes the form data when submitting it with NativeForm.Method.POST.
      Parameters:
      enctype - the encoding, or null to remove the attribute, in which case the browser uses NativeForm.Enctype.URLENCODED
    • getEnctype

      public @Nullable NativeForm.Enctype getEnctype()
      Gets how the browser encodes the form data when submitting it.
      Returns:
      the encoding, or null if not set or not recognized
      See Also:
    • setTarget

      public void setTarget(@Nullable AnchorTargetValue target)
      Sets the window, tab or frame that shows the response when the form is submitted. Use AnchorTarget.BLANK to keep the Vaadin UI open while the response opens in a new tab, or AnchorTargetValue.forString(String) with the name of an IFrame to show it there.
      Parameters:
      target - the target, or null or AnchorTarget.DEFAULT to remove the attribute, in which case the response replaces the current page
    • getTarget

      public @Nullable AnchorTargetValue getTarget()
      Gets the window, tab or frame that shows the response when the form is submitted.
      Returns:
      the target, or null if not set
      See Also:
    • add

      public void add(Collection<com.vaadin.flow.component.Component> components)
      Throws:
      IllegalArgumentException - if one of the components is a NativeForm
    • addComponentAtIndex

      public void addComponentAtIndex(int index, com.vaadin.flow.component.Component component)
      Throws:
      IllegalArgumentException - if the component is a NativeForm
    • replace

      public void replace(@Nullable com.vaadin.flow.component.Component oldComponent, @Nullable com.vaadin.flow.component.Component newComponent)
      Throws:
      IllegalArgumentException - if the new component is a NativeForm
    • bindChildren

      public <V extends @Nullable Object, S extends com.vaadin.flow.signals.Signal<V>> void bindChildren(com.vaadin.flow.signals.Signal<List<S>> list, com.vaadin.flow.function.SerializableFunction<S,com.vaadin.flow.component.Component> childFactory)

      A NativeForm created by the factory is rejected with an IllegalArgumentException. While this form is attached, the factory runs in an effect, so the exception is passed to the session error handler instead of being thrown to the caller.