Friday, June 5, 2015

GWTBootstrap - custom email validation for TextBox

=== JAVA CODE ===

TextBox emailLookup = new TextBox();
emailLookup.addValidator(new Validator() {
            @Override
            public int getPriority() {
                return 0;
            }

            @Override
            public List<EditorError> validate(Editor editor, Object value) {
                List<EditorError> result = new ArrayList<EditorError>();
                String valueStr = value == null ? "" : value.toString();
                if (!valueStr.matches(EMAIL_REGEXP)) {
                    result.add(new BasicEditorError(emailLookup, value, "Not a valid email address"));
                } else if (isEmailReapeated(valueStr)) {
                    result.add(new BasicEditorError(emailLookup, value, "Email address is repeated"));
                }
                return result;
            }
        });

=== UI BINDER ===

<b:Row addStyleNames="{style.padding}">
    <b:Column size="XS_12">
        <b:Row>
            <b:Column size="XS_2">
                <b:FormGroup>
                    <b:TextBox ui:field="emailLookup" placeholder="Enter email" allowBlank="false"/>
                    <b:InlineHelpBlock iconType="EXCLAMATION"/>
                </b:FormGroup>
            </b:Column>

            <b:Column size="XS_1">
                <b:Button ui:field="addContact" text="Add"/>
            </b:Column>

        </b:Row>
    </b:Column>
</b:Row>

No comments: