Set maxLength attribute on ComboBox

Viewed 36

I want to add a maxLength to the custom value on the ComboBox but when I do

getElement().setAttribute("maxLength","30");

Vaadin add it to the wrapping vaadin-combo-box element. How can I access from Java the underlying highlighted input element as show in the picture?

enter image description here

3 Answers

Applying a max-length on the client-side does not guarantee that the value on the server-side is secured.

Instead of mingling with the inner workings of the ComboBox's field, I would suggest to apply this validation on the server-side. In the ComboBox::addCustomValueSetListener or by using a StringLengthValidator on the Binder you have attached to the form/field.

I found out that you can executejs on client side through element API. I solved using this snippet:

element.executeJs("this.childNodes.item(0).maxLength = 30")

"childNodes.item(0)" is the underlying input element of combobox

Related