Validating Text field without using Vaadin Form

I have several fields added to a panel and i want to on a button click check the fields that are required as a means of validation. I understand that if using a Form element, then all i need to do is call Commit on each component which will validate the required fields. I want to accomplish the same thing but i am not using a Vaadin Form.

Can i get all the components in my layout and then loop through each component and check if the required components are empty, then i can handle it in whatever way i want?

How do i iterate over each component in an AbsoluteLayout ?

Using layout.getComponentIterator() ?

Why not use a form and set a different layout?

I am using EclipseLInk JPA as my ORM provider and i couldn’t get Vaadin form to work with it. I ended up adding each textfield to a set<textfield> and checked the isValid method as a workaround.

The component iterator returns all components in the layout and i was only looking for the textfields.

You can check the type of field by saying:

if (iterator.next() instanceof TextField) {
...
}

or save references to your text fields into your own list.

This is actually what i ended up doing, thanks for the help

You can pass layout with components needed to validate to something like this


protected boolean validate(ComponentContainer layout) {
        boolean result = true;
        Iterator<Component> i = layout.getComponentIterator();
        while (i.hasNext()) {
            Component c = i.next();
            if (c instanceof AbstractField) {
                try {
                    ((AbstractField) c).validate();
                } catch (Exception e) {
                    ((AbstractComponent)c).setComponentError(new UserError(e.getMessage()));
                    result = false;
                }
            } else if (c instanceof AbstractComponentContainer) {
                if (!validate((AbstractComponentContainer) c)) {
                    result = false;
                }
            }
        }
        return result;
    }

Could you please how to pass layout with componets ?

Suppose I have a layout with certain elements, how to pass the layout.

[code]

[code]
[code]
[code]
VerticalLayout layout = new VerticalLayout()

TextField user =new TextField("UserName");
TextField name =new TextField("Name");

layout.addComponent(user);
layout.addComponent(name);
[/code]
[/code]
[/code]

[/code]how to pass the layout here ???