Basic form validation

Hi

I am having issues with getting Form validations done. I am creating my own components like text fields etc using new () operator and am adding it to the form.

form = new Form();
form.setImmediate(true);
form.setValidationVisible(true);

firstName = new TextField(“First name”);
firstName.setImmediate(true);
firstName.setRequired(true);
firstName.setRequiredError(“First name is mandatory”);
form.getLayout().addComponent(firstName);

I also create the button as

Button btn = new Button(“Save”,form,“commit”);

I am NOT using the form.setItemDataContainer() method to bind a pojo. All I want to get done is that the required fields get validated on clicking of the save button.

Somehow validation does not seem to work.

Is it mandatory that i need a form bound to a container for validations to work ?

Any tips on getting this right ?

-tia
jv

Looks like replacing

form.getLayout().addComponent(firstName);

with

form.addField(“firstName”,firstName);

does the trick.

Edit:
Also I manually call form.validate() on click of the button