Skip to content

Instantly share code, notes, and snippets.

@jca02266
Last active May 15, 2018 09:06
Show Gist options
  • Save jca02266/d2d9849eeb093d7cd5e53c64353a4b1f to your computer and use it in GitHub Desktop.
Save jca02266/d2d9849eeb093d7cd5e53c64353a4b1f to your computer and use it in GitHub Desktop.
GWT validator sample
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.7.0//EN"
"http://gwtproject.org/doctype/2.7.0/gwt-module.dtd">
<module rename-to="App">
<inherits name="com.google.gwt.user.User"/>
<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with class="sample.webapp.client.ValidatorFactory">
<when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>
<replace-with class="sample.webapp.client.validation.ValidationMessagesResolver">
<when-type-is class="com.google.gwt.validation.client.UserValidationMessagesResolver" />
</replace-with>
<entry-point class="sample.webapp.client.App"/>
<extend-property name="locale" values="ja"/>
<source path="client" />
<source path="shared" />
</module>
package sample.webapp.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.validation.client.impl.Validation;
import sample.webapp.shared.Person;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
public class App extends Composite implements EntryPoint {
interface Binder extends UiBinder<Widget, App> {
}
private final Binder binder = GWT.create(Binder.class);
@UiField
TextBox name;
@UiField
FlowPanel outDiv;
public App() {
initWidget(binder.createAndBindUi(this));
}
@Override
public void onModuleLoad() {
RootPanel.get().add(this);
}
@UiHandler("sayHi")
void onClickMe(ClickEvent event) {
Person person = new Person();
person.name = name.getText();
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
for (ConstraintViolation<Person> violation: validator.validate(person)) {
GWT.log(violation.getMessage());
}
outDiv.add(new Label("Hi " + name.getValue() + "!"));
}
}
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'
>
<g:HTMLPanel>
<h1>Welcome</h1>
<div>nama:</div>
<g:TextBox ui:field="name" />
<g:Button ui:field="sayHi">Say hi!</g:Button>
<g:FlowPanel ui:field="outDiv" />
</g:HTMLPanel>
</ui:UiBinder>
dependencies {
compile 'org.hibernate:hibernate-validator:4.1.0.Final'
compile 'org.hibernate:hibernate-validator:4.1.0.Final:sources'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.5.6'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
}
<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/Code">
<head>
<title>Putnami gradle plugin - sample</title>
<script type="text/javascript" src="App/App.nocache.js"></script>
</head>
<body>
<noscript>Your browser does not support JavaScript!</noscript>
</body>
</html>
package sample.webapp.shared;
import javax.validation.constraints.Size;
public class Person {
@Size(min = 4, message="{custom.message}")
public String name;
}
package sample.webapp.shared.validation;
public interface ValidationMessages extends org.hibernate.validator.ValidationMessages {
@DefaultStringValue("characters long.")
@Key("custom.message")
String custom_message();
}
javax.validation.constraints.AssertFalse.message=must be false
javax.validation.constraints.AssertTrue.message=must be true
javax.validation.constraints.DecimalMax.message=must be less than or equal to {value}
javax.validation.constraints.DecimalMin.message=must be greater than or equal to {value}
javax.validation.constraints.Digits.message=numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)
javax.validation.constraints.Future.message=must be in the future
javax.validation.constraints.Max.message=must be less than or equal to {value}
javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.NotNull.message=may not be null
javax.validation.constraints.Null.message=must be null
javax.validation.constraints.Past.message=must be in the past
javax.validation.constraints.Pattern.message=must match "{regexp}"
javax.validation.constraints.Size.message=SIZE must be between {min} and {max}
org.hibernate.validator.constraints.Email.message=not a well-formed email address
org.hibernate.validator.constraints.Length.message=length must be between {min} and {max}
org.hibernate.validator.constraints.NotBlank.message=may not be empty
org.hibernate.validator.constraints.NotEmpty.message=may not be empty
org.hibernate.validator.constraints.Range.message=must be between {min} and {max}
org.hibernate.validator.constraints.URL.message=must be a valid URL
org.hibernate.validator.constraints.CreditCardNumber.message=invalid credit card number
org.hibernate.validator.constraints.ScriptAssert.message=script expression "{script}" didn't evaluate to true
custom.message=サイズ {min} and {max}
javax.validation.constraints.AssertFalse.message=must be false
javax.validation.constraints.AssertTrue.message=must be true
javax.validation.constraints.DecimalMax.message=must be less than or equal to {value}
javax.validation.constraints.DecimalMin.message=must be greater than or equal to {value}
javax.validation.constraints.Digits.message=numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)
javax.validation.constraints.Future.message=must be in the future
javax.validation.constraints.Max.message=must be less than or equal to {value}
javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.NotNull.message=may not be null
javax.validation.constraints.Null.message=must be null
javax.validation.constraints.Past.message=must be in the past
javax.validation.constraints.Pattern.message=must match "{regexp}"
javax.validation.constraints.Size.message=サイズは{min}と{max}の間でなければいけません
org.hibernate.validator.constraints.Email.message=not a well-formed email address
org.hibernate.validator.constraints.Length.message=length must be between {min} and {max}
org.hibernate.validator.constraints.NotBlank.message=may not be empty
org.hibernate.validator.constraints.NotEmpty.message=may not be empty
org.hibernate.validator.constraints.Range.message=must be between {min} and {max}
org.hibernate.validator.constraints.URL.message=must be a valid URL
org.hibernate.validator.constraints.CreditCardNumber.message=invalid credit card number
org.hibernate.validator.constraints.ScriptAssert.message=script expression "{script}" didn't evaluate to true
custom.message=サイズ {min} and {max}
package sample.webapp.client.validation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.ConstantsWithLookup;
import com.google.gwt.validation.client.AbstractValidationMessageResolver;
import com.google.gwt.validation.client.UserValidationMessagesResolver;
import sample.webapp.shared.validation.ValidationMessages;
public class ValidationMessagesResolver extends AbstractValidationMessageResolver implements UserValidationMessagesResolver {
protected ValidationMessagesResolver() {
super((ConstantsWithLookup) GWT.create(ValidationMessages.class));
System.out.println("テスト");
}
}
package sample.webapp.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.validation.client.AbstractGwtValidatorFactory;
import com.google.gwt.validation.client.GwtValidation;
import com.google.gwt.validation.client.impl.AbstractGwtValidator;
import sample.webapp.shared.Person;
import javax.validation.Validator;
public final class ValidatorFactory extends AbstractGwtValidatorFactory {
/**
* Validator marker for the Validation Sample project. Only the classes and groups listed
* in the {@link GwtValidation} annotation can be validated.
*/
@GwtValidation(Person.class)
public interface GwtValidator extends Validator {
}
@Override
public AbstractGwtValidator createValidator() {
return GWT.create(GwtValidator.class);
}
}
@andrewsleeman24
Copy link

andrewsleeman24 commented Nov 15, 2017

Thank you, thank you, thank you.
I have looked at GWT-Validation many times unsuccessfully over the past few years.
With this example (along with the following maven example) I was able to get my implementation working.
https://github.com/gwtproject/gwt/blob/master/samples/validation/pom.xml
NB. another secret appears to be to use "jar xvf .../hibernate-validator-4.1.0.Final-sources.jar" and "jar xvf .../hibernate-validator-4.1.0.Final-sources.jar *" to repackage the jar download from the maven repository to enable gwt to compile successfully (atleast with gwt-2.8.2 with java 1.8.0_171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment