Build your contact form

It is good to keep your designs focused on one thing. When there are too many focus areas in one view, splitting them into smaller parts helps to keep things clear. We will create the form in its own design with a Form Layout for editing the information of a single contact in it.

Creating a new design for the form

  1. Switch back to IntelliJ IDEA.

  2. In the Project tree, right click the frontend/src/views folder and select New > Vaadin 10+ Design.

  3. In the Name field, type contact-form.

  4. Make sure that

    1. the Create Java companion file checkbox is checked.

    2. the Java package is com.vaadin.tutorial.crm.ui.

  5. Click OK to generate the files.

  6. The design opens in the browser.

  7. In the palette, search for "vaadin-form" and drag vaadin-form-layout onto the drop section in the paper.

Our form component will have:

  • Two text fields for the first and last name.

  • An email field.

  • Two select fields: one to select the company and the other to select the contact status.

Adding two text fields for the name

  1. Find Vaadin Text Field in the palette and drag it twice onto vaadin-form-layout.

  2. Select the first vaadin-text-field in the design. In the properties view, go to the attributes panel. Find the label attribute and replace the text with "First name". Find the placeholder attribute and remove the value.

  3. Select the second vaadin-text-field in the design. In the properties view, go to the attributes panel. Find the label attribute and replace the text with "Last name". Find the placeholder attribute and remove the value.

Note
If your paper is wide, the text fields will line up horizontally. You can drag the left and right paper edges to make it narrower. This will make the form fields stack vertically. This behavior comes from vaadin-form-layout by default, which arranges its fields into one or two columns depending on the width.

Adding the email field

  1. In the palette, find vaadin-email-field and drag it onto the vaadin-form-layout.

  2. In the properties view, go to the attributes panel. Find the label attribute and type in "Email".

Adding company and status fields

  1. In the palette, find vaadin-combo-box and drag it onto the vaadin-form-layout twice.

  2. Select the first vaadin-combo-box in the design. In the properties view, go to the attributes panel. Find the label attribute and add the value "Company".

  3. Select the second vaadin-combo-box in the design. In the properties view, go to the attributes panel. Find the label attribute and add the value "Status".

Adding buttons

We need to add save, delete and close buttons to the form. Save is the primary action and should be highlighted. Delete can be a dangerous action and should be marked as an error. Close is not an important action and can be downplayed with a tertiary styling.

  1. In the palette, find Horizontal Layout Spacing and drag it onto the vaadin-form-layout.

  2. In the Vaadin button section of the palette, drag the following three buttons onto the horizontal layout: Primary Button, then Primary Error Button next to it, and finally Tertiary Button next to that.

  3. Select the Primary button and change the button text to "Save".

  4. Select the Primary Error button and change the button text to "Delete". In the attributes panel, change the theme attribute from "primary error" to "error".

  5. Select the Tertiary button and change the button text to "Close".

Now, our form is done.

Adding the form to the main view

We can now go back to our main view and add the contact form next to the Grid. The main-view design should be open in a second Chrome tab. If it is not, reopen the main-view.js file again in IntelliJ IDEA.

In the palette, you should now see a section called Project Components that lists all designs you have created. By dragging items from this list, you can embed other designs into the one that is currently open.

  1. In the palette of the main-view Designer, locate contact-form in Project Components.

  2. Drag contact-form onto the second vaadin-horizontal-layout.

Sizing the grid and form

We want to give most of the space to the grid. For this, we use flex sizing to set a 2:1 ratio between the grid and the form.

  1. Select the vaadin-grid. In the properties view, find the style attribute in the attributes panel and add the value "flex: 2;" at the end.

  2. Select contact-form. In the properties view, find the style attribute in the attributes panel and set the value to "flex: 1;".

Now, our layout is done.

Proceed to the next chapter to run your application: Get your application up and running