GWT编辑器框架:实现HasEditorErrors的自定义LeafValueEditor

时间:2012-01-22 11:20:00

标签: gwt validation bean-validation

我已将我自己的表单字段实现为IsEditor<LeafValueEditor<String>>,我希望以我的应用程序的形式使用。

public class FormField extends Composite implements IsEditor<LeafValueEditor<String>> {

    interface FormFieldUiBinder extends UiBinder<Widget, FormField> {
    }

    private static FormFieldUiBinder uiBinder = GWT.create(FormFieldUiBinder.class);

    interface FormFieldStyle extends CssResource {
        String error();
    }

    @UiField
    TextBox wrapped;

    private String placeholder;

    public FormField() {
        initWidget(uiBinder.createAndBindUi(this));
        wrapped.setTitle("");
    }

    @UiHandler("wrapped")
    public void onFocus(FocusEvent event) {
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
                wrapped.selectAll();
            }
        });
    }

    public String getText() {
        return wrapped.getText();
    }

    public void setText(String text) {
            wrapped.setText(text);
    }

    /**
     * Gets the current placeholder text for the text box.
     * 
     * @return the current placeholder text
     */
    public String getPlaceholder() {
        return placeholder;
    }

    /**
     * Sets the placeholder text displayed in the text box.
     * 
     * @param placeholder
     *            the placeholder text
     */
    public void setPlaceholder(String text) {
        placeholder = (text != null ? text : "");
        wrapped.getElement().setPropertyString("placeholder", placeholder);
    }

    public String getTitle() {
        return wrapped.getTitle();
    }

    public void setTitle(String title) {
        wrapped.setTitle(title);
    }

    @Override
    public LeafValueEditor<String> asEditor() {
        return wrapped.asEditor();
    }

    public int getVisibleLength() {
        return wrapped.getVisibleLength();
    }

    public void setVisibleLength(int length) {
        wrapped.setVisibleLength(length);
    }

    public boolean isReadOnly() {
        return wrapped.isReadOnly();
    }

    public void setReadOnly(boolean readOnly) {
        wrapped.setReadOnly(readOnly);
    }

    public boolean isEnabled() {
        return wrapped.isEnabled();
    }

    public void setEnabled(boolean enabled) {
        wrapped.setEnabled(enabled);
    }

    public void setWidth(String width) {
        wrapped.setWidth(width);
    }

}

相应的UIBinder文件很简单:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
    xmlns:g='urn:import:com.google.gwt.user.client.ui'>

    <g:HTMLPanel>
        <g:TextBox ui:field="wrapped" />
    </g:HTMLPanel>
</ui:UiBinder>

这在我创建的表单中顺利运行:

<g:AbsolutePanel width="350px" height="225px"
    styleName="{res.css.inputArea}">
    <g:at left='10' top='0'>
        <g:HTMLPanel width="350px">
            <h1>Personalia</h1>
        </g:HTMLPanel>
    </g:at>
    <g:at left='10' top='65'>
        <f:FormLabel text="voornaam" />
    </g:at>
    <g:at left='10' top='80'>
        <f:FormField ui:field="firstName" placeholder="voornaam" />
    </g:at>
    <g:at left='10' top='115'>
        <f:FormLabel text="achternaam" />
    </g:at>
    <g:at left='10' top='130'>
        <f:FormField ui:field="lastName" placeholder="achternaam"/>
    </g:at>
</g:AbsolutePanel>

在我的观点中,我可以像这样使用EditorDriver:

interface EditorDriver extends SimpleBeanEditorDriver<Account, AccountPersonaliaEditor> {
}
private final EditorDriver editorDriver = GWT.create(EditorDriver.class);

填充表单也可以正常工作

    editorDriver.initialize(editor);
    editorDriver.edit(presenter.getAccount());

除了在编辑后获取值:

Account account = editorDriver.flush();

现在我想对错误实施反馈。我有 GWT Bean验证框架也正常工作。我只需要显示错误。

所以,我接下来要做的是让FormField 实现HasEditorErrors 。这是我的问题/问题。

public class FormField extends Composite implements IsEditor<LeafValueEditor<String>>, HasEditorErrors<String> 

一旦我实现了这个接口(即使是一个空的实现),我遇到了以下编译时错误:

[DEBUG] [klawtapp] - Rebinding com.example.screen.ui.center.AccountPersonaliaImpl.EditorDriver
    [DEBUG] [klawtapp] - Invoking generator com.google.gwt.editor.rebind.SimpleBeanEditorDriverGenerator
        [DEBUG] [klawtapp] - Creating Editor model for com.example.screen.ui.center.AccountPersonaliaImpl.EditorDriver
            [DEBUG] [klawtapp] - Descending into firstName
                [ERROR] [klawtapp] - Could not find a getter for path wrapped in proxy type java.lang.String
            [DEBUG] [klawtapp] - Descending into lastName
                [ERROR] [klawtapp] - Could not find a getter for path wrapped in proxy type java.lang.String
            [ERROR] [klawtapp] - Unable to create Editor model due to previous errors
[ERROR] [klawtapp] - Deferred binding failed for 'com.example.screen.ui.center.AccountPersonaliaImpl.EditorDriver'; expect subsequent failures

这似乎是微不足道的。我已经尝试添加getter / setter for wrapped,但这并没有真正帮助。

编辑:片刻,我认为解决方案是实现HasEditorErrors<LeafValueEditor<String>>而不是HasEditorErrors<String>,以防止将层次结构降级到包装的TextBox,但结果是类似:

            [ERROR] [klawtapp] - Could not find a getter for path wrapped in proxy type com.google.gwt.editor.client.LeafValueEditor

1 个答案:

答案 0 :(得分:1)

只需使用wrapped注释@Editor.Ignore文本框即可。

或者,您可以移除implements IsEditor<LeafValueEditor<String>>,然后使用wrapped注释@Path("")字段(您必须使用null值进行测试面对他们,因为我不确定它是否会按原样运作。

或者您可以选择实施自己的LeafValueEditor<String>,而不是依赖TextBox中的{。}}。