Class NativeForm

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasComponents, HasComponentsOfType<Component>, HasElement, HasEnabled, HasSize, HasStyle, HasText, Serializable

@NullMarked @Tag("form") public class NativeForm extends 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.

Since:
25.4
Author:
Vaadin Ltd
See Also:
  • Constructor Details

  • 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<Component> components)
      Adds the given components as children of this component.

      In case any of the specified components has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      components - the components to add
      Throws:
      IllegalArgumentException - if one of the components is a NativeForm
    • addComponentAtIndex

      public void addComponentAtIndex(int index, Component component)
      Adds the given component as a child of this component at the specific index.

      In case the specified component has already been added to another parent, it will be removed from there and added to this one.

      Parameters:
      index - the index, where the component will be added. The index must be non-negative and may not exceed the children count
      component - the component to add, value should not be null
      Throws:
      IllegalArgumentException - if the component is a NativeForm
    • replace

      public void replace(@Nullable Component oldComponent, @Nullable Component newComponent)
      Replaces the component in the container with another one without changing position. This method replaces a component with another one is such a way that the new component overtakes the position of the old component. If the old component is not in the container, the new component is added to the container. If both components are already in the container, their positions are swapped. Component attach and detach events should be taken care as with add and remove.
      Parameters:
      oldComponent - the old component that will be replaced. Can be null, which will make the newComponent to be added to the layout without replacing any other
      newComponent - the new component to be replaced. Can be null, which will make the oldComponent to be removed from the layout without adding any other
      Throws:
      IllegalArgumentException - if the new component is a NativeForm
    • bindChildren

      public <V extends @Nullable Object, S extends Signal<V>> void bindChildren(Signal<List<S>> list, SerializableFunction<S,Component> childFactory)
      Binds a list Signal to this component using a child component factory. Each item Signal in the list corresponds to a child component within this component.

      This component is automatically updated to reflect the structure of the list. Changes to the list, such as additions, removals, or reordering, will update this component's children accordingly.

      This component must not contain any children in the default slot (i.e. without a slot attribute) that are not part of the list. If this component has existing default-slot children when this method is called, or if it contains unrelated default-slot children after the list changes, an IllegalStateException will be thrown. Named-slot children are allowed and can be added or removed freely while the binding is active.

      New child components are created using the provided childFactory function. This function takes a Signal from the list and returns a corresponding child component. It shouldn't return null. The Signal can be further bound to the returned component as needed. Note that childFactory is run inside a Effect, and therefore Signal.get() calls makes effect re-run automatically on signal value change.

      Example of usage:

       SharedListSignal<String> taskList = new SharedListSignal<>(String.class);
      
       UnorderedList component = new UnorderedList();
      
       component.bindChildren(taskList, ListItem::new);
       

      Note: The default implementation adds children directly to the component?s element using the Element API and does not invoke HasComponentsOfType.add(Component...). Components that override add or manage children indirectly must override this method to provide a suitable implementation or explicitly disable it.

      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.

      Type Parameters:
      V - the value type of the Signals in the list
      S - the type of the Signals in the list
      Parameters:
      list - list signal to bind to the parent, must not be null
      childFactory - factory to create new component, must not be null