Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
ComboBox validation ( setRequired ) not working in customized form
Hi All,
I have form with customized form field factory, one of the elements is a ComboBox.
In this form, the combobox input is mandatory. so i used the setRequired(true);
The following is the code snippet for the combo box creation.
class BasicFormFieldFactory extends DefaultFieldFactory {
String PROPERTY_CAPTION_NAME = "name";
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
if (propertyId.equals("data2")) {
ComboBox cbox = new ComboBox();
cbox.setCaption("Test");
cbox.setWidth("100px");
cbox.setItemCaptionPropertyId(PROPERTY_CAPTION_NAME);
cbox.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
cbox.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);
cbox.setContainerDataSource(getContainer());
cbox.setImmediate(true);
cbox.setNullSelectionAllowed(false);
cbox.setRequired(true);
cbox.setRequiredError("Select a value.");
return cbox;
}
TextField tf = new TextField();
tf.setCaption((String) propertyId);
return tf;
}
private Container getContainer() {
IndexedContainer container = new IndexedContainer();
container.addContainerProperty(PROPERTY_CAPTION_NAME, String.class, null);
for (int i = 0; i < 10; i++) {
Item systemItem = container.addItem("i" + i);
systemItem.getItemProperty(PROPERTY_CAPTION_NAME).setValue("Data " + i);
}
return container;
}
}
the form is set to immediate, as well as the combobox.
but still the form is getting submitted without any inputs from combobox.
Not sure what is wrong in my approach.
Attaching a test case for the same. The user is able to submit the form without selecting a value from the combobox, which should not be happening.
Thanks n Regardz
Vasu Patnaik.
I guess this is because the ComboBox is initially rendered without a selection, if no selection is made explicitly on the server side, even when the ComboBox has setNullSelectionAllowed(false).
I would consider this a severe bug, since traditional native select components always render the select with a default selection, usually the first item in the list.
Hi Jouni Koivuviita,
Jouni Koivuviita: I guess this is because the ComboBox is initially rendered without a selection, if no selection is made explicitly on the server side, even when the ComboBox has setNullSelectionAllowed(false).
I would consider this a severe bug, since traditional native select components always render the select with a default selection, usually the first item in the list.
Thanks for the reply.
I have raised a ticket regarding this at this location - > http://dev.vaadin.com/ticket/4640
Thanks n Regardz
Vasu Patnaik.
The issue is not the combobox or the null value but that addField is overriden and fails to call registerField to register the property to the form. As there are no properties registered, validation will not validate anything. You probably meant to override attachField (used to add the field to the layout) instead of addField.
I have a big problem with validation in combobox. It is created by FieldFactory. And validation doesn't work for this field.
It's a little strange because the validation works when if you set something and return empty value back. But in initial state not working.
If I understood the property will validate If It was registered.
Form.class
public boolean addItemProperty(Object id, Property property) {
// Checks inputs
if (id == null || property == null) {
throw new NullPointerException("Id and property must be non-null");
}
// Checks that the property id is not reserved
if (propertyIds.contains(id)) {
return false;
}
propertyIds.add(id);
ownProperties.put(id, property);
// Gets suitable field
final Field field = fieldFactory.createField(this, id, this);
if (field == null) {
return false;
}
// Configures the field
field.setPropertyDataSource(property);
// Register and attach the created field
addField(id, field);
return true;
}
public void addField(Object propertyId, Field field) {
registerField(propertyId, field);
attachField(propertyId, field);
requestRepaint();
}
Source code shows that combobox field will be registered anyway.
Where do I have mistakes?