GWT 2.5.0 を使用して、クライアント側の検証とエディターを使用したいと考えています。次のように ConstraintViolation java.util.Set を EditorDriver に渡そうとすると、次のエラーが発生します。
Validator a = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Person>> b = a.validate(person);
editorDriver.setConstraintViolations(b);
The method setConstraintViolations(Iterable<ConstraintViolation<?>>) in the type EditorDriver<Person> is not applicable for the arguments (Set<ConstraintViolation<Person>>)
私が見つけることができた唯一の関連記事はIssue 6270でした!
以下は、名前を指定して注釈に対して検証できる Person Editor を備えた PopUpDialog を表示する例です。personDriver.setConstraintViolations(violations);PersonEditorDialogの行をコメントアウトすると、例を実行できます。
例の画像を投稿するのに十分な評判ポイントがありません。
クラス
人
public class Person {
@NotNull(message = "You must have a name")
@Size(min = 3, message = "Your name must contain more than 3 characters")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PersonEditorDialog
public class PersonEditorDialog extends DialogBox implements Editor<Person> {
private static PersonEditorDialogUiBinder uiBinder = GWT
        .create(PersonEditorDialogUiBinder.class);
interface PersonEditorDialogUiBinder extends
        UiBinder<Widget, PersonEditorDialog> {
}
private Validator validator;
public PersonEditorDialog() {
    validator = Validation.buildDefaultValidatorFactory().getValidator();
    setWidget(uiBinder.createAndBindUi(this));
}
interface Driver extends SimpleBeanEditorDriver<Person, PersonEditorDialog> {
};
@UiField
ValueBoxEditorDecorator<String> nameEditor;
@UiField
Button validateBtn;
private Driver personDriver;
@UiHandler("validateBtn")
public void handleValidate(ClickEvent e) {
    Person created = personDriver.flush();
    Set<ConstraintViolation<Person>> violations = validator
            .validate(created);
    if (!violations.isEmpty() || personDriver.hasErrors()) {
        StringBuilder violationMsg = new StringBuilder();
        for (Iterator<ConstraintViolation<Person>> iterator = violations.iterator(); iterator.hasNext();) {
            ConstraintViolation<Person> constraintViolation = (ConstraintViolation<Person>) iterator
                    .next();
            violationMsg.append(constraintViolation.getMessage() + ",");
        }
        Window.alert("Detected violations:" + violationMsg);
         personDriver.setConstraintViolations(violations);
    }
}
@Override
public void center() {
    personDriver = GWT.create(Driver.class);
    personDriver.initialize(this);
    personDriver.edit(new Person());
    super.center();
}
}
SampleValidationFactory
public final class SampleValidationFactory 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);
}
}
EditorValidationTest
public class EditorValidationTest implements EntryPoint {
/**
 * This is the entry point method.
 */
public void onModuleLoad() {
    PersonEditorDialog personEditorDialog = new PersonEditorDialog();
    personEditorDialog.center();
}
}
UiBinder
PersonEditorDialog.ui.xml
<!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" xmlns:e="urn:import:com.google.gwt.editor.ui.client">
<ui:style>
    .important {
        font-weight: bold;
    }
</ui:style>
<g:HTMLPanel>
    <g:Label>Enter your Name:</g:Label>
    <e:ValueBoxEditorDecorator ui:field="nameEditor">
        <e:valuebox>
            <g:TextBox />
        </e:valuebox>
    </e:ValueBoxEditorDecorator>
    <g:Button ui:field="validateBtn">Validate</g:Button>
</g:HTMLPanel>
</ui:UiBinder> 
GWT モジュール
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='editorvalidationtest'>
<inherits name='com.google.gwt.user.User' />
<inherits name='com.google.gwt.user.theme.clean.Clean' />
<inherits name="com.google.gwt.editor.Editor"/>
<!-- Validation module inherits -->
<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with
    class="com.test.client.SampleValidationFactory">
    <when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>
<!-- Specify the app entry point class. -->
<entry-point class='com.test.client.EditorValidationTest' />
<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />
</module>
クラスパスで必要なライブラリ
- hibernate-validator-4.1.0.Final.jar
- hibernate-validator-4.1.0.Final-sources.jar
- validation-api-1.0.0.GA.jar (GWT SDK 内)
- validation-api-1.0.0.GA-sources.jar (GWT SDK 内)
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
- log4j-1.2.16.jar