Binding Data to Forms
Hilla provides a way for binding input fields to a data model.
The client-side Binder supports Java backend endpoints for loading and saving the form data, and reuses the metadata from Java Bean validation annotations for client-side validation.
API Basics
The form binding API consists of three key concepts:
-
The
field()directive to bind the field components in form views -
The generated TypeScript models for POJO classes used in endpoints, which are used as field references and provide the necessary metadata
-
The
useFormReact Hook returns aUseFormResultobject that is responsible for keeping track of the form state, the default and current values, and validation of the data.
See the Form Binding Reference for more details.
How to Bind Form Data
For example, let us consider a Java endpoint with methods for loading and saving a Person bean:
Source code
Java
/**
* A Hilla endpoint for the person-view.ts form view.
*/
@Endpoint
public class PersonEndpoint {
/**
* Loads a Person to edit into the view.
* @return default form data
*/
public Person loadPerson() {
// ...
}
/**
* Saves the edited Person from the view.
* @param person form data to save
*/
public void savePerson(Person person) {
// ...
}
}To bind data to a form, follow these steps in your
frontend/views/person/PersonView.tsx client-side React view:
-
Import the
useFormhook from the@vaadin/hilla-react-formpackage. Import yourPersonEndpointdata endpoint and the generatedPersonModelfrom thefrontend/generatedfolder:Source code
tsx
import { useForm } from '@vaadin/hilla-react-form'; import { PersonEndpoint } from 'Frontend/generated/endpoints'; import PersonModel from 'Frontend/generated/com/example/application/PersonModel'; -
Acquire a
UseFormResultinstance for your view by calling theuseForm:Source code
tsx
export default function PersonView() { // ... const { model, field } = useForm(PersonModel); // ... }The
PersonModelhere is generated alongside aPersonTypeScript data interface from thePerson.javabean. This describes the structure of the data and the validation-related metadata for the form binding. -
Bind the UI components in the template using the
{…field()}syntax:Source code
tsx
export default function PersonView() { // ... const { model, field } = useForm(PersonModel); return ( <TextField label="Full name" {...field(model.fullName)} /> ); }In this example,
modelis an instance ofPersonModel.NoteModels don’t contain any actual data. To access the actual current or default value of the form, you can acquire their respective reference by destructing the UseFormResultinstance asconst { value, defaultValue, … } = useForm(…)when calling theuseFormhook.