Stephane Grenier
Stephane Grenier

Reputation: 15925

Why use FormLayout instead of a VerticalLayout in Vaadin?

With Formlayout all your captions are to the left of the field. This is fine if you only have forms with one column of fields, but as soon as you have more complex forms such as a Firstname and Lastname on the same row that you want to put side by side, you can't use FormLayout, at least not if you want captions for the fields to be consistent, either left or above the fields. Yes you can wrap them in an HorizontalLayout and set an InputPrompt in a FormLayout, but that doesn't always work for more complex forms because sometimes you still need the caption. With a VerticalLayout at least all your captions will be consistent.

All that to say what's the advantage of using a FormLayout over a VerticalLayout? I want all the captions to be consistent, either top or left, and if I use anything but a single column of fields than I'm out of luck. I believe validation can also be done outside of a FormLayout so I'm not sure this is a pro limited to the FormLayout.

If I just used a VerticalLayout then I get all the captions above the field, and this will be consistent for all fields, even if I have to do any advanced layouts and combine many different types of Layouts, such as Firstname and Lastname on the same row, or an address row where I want the city and Zip code fields to be different sizes, etc.

There must be an advantage to the FormLayout over the VerticalLayout but I can't see it...

Upvotes: 1

Views: 642

Answers (1)

kukis
kukis

Reputation: 4644

There really isn't much into it. FormLayout was used by deprecated now com.vaadin.ui.Form. We can speculate that the class was probably created to provide default settings for the com.vaadin.ui.Form.

Differences are stated in JavaDoc too:

  • captions are on the left
  • validation is shown in between the caption and the component
  • difference in default spacing and margins settings

You can also take a look at the server side code of both classes. It looks very similar and therefore the logic side of both layouts is exactly the same.

Source: the JavaDoc and my own experience.

Upvotes: 4

Related Questions