Search code examples
htmlgwtuibinder

How to generate 'required' Attribute in GWT using UIBinder?


Expected HTML

<input class="sth" type="input" required/>

How to generate this using UIBinder in GWT? I am using following and not generating the required attribute

<g:TextBox ui:field="username"/>

Solution

  • You cannot do it in UiBinder with standard TextBox widget. If this is a one-off situation, set it in code. If this is something that you use often, create your own widget by extending TextBox and adding:

    public void setRequired(boolean isRequired) {
        if (isRequired) {
            getElement().setAttribute("required", "required");
        }
    }
    

    Then you can use it in UiBinder:

    <w:MyTextBox ui:field="username" required="true"/>
    

    where w links to your widgets folder.