【问题标题】:GWT Editor Framework : custom LeafValueEditor implementing HasEditorErrorsGWT 编辑器框架:自定义 LeafValueEditor 实现 HasEditorErrors
【发布时间】:2012-01-22 11:20:00
【问题描述】:

我已经将自己的表单字段实现为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,但这并没有真正帮助。

编辑:一会儿,我认为解决方案是实现HasEditorErrors&lt;LeafValueEditor&lt;String&gt;&gt; 而不是HasEditorErrors&lt;String&gt; 以防止层次结构下降到包装的TextBox,但结果是相似的:

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

【问题讨论】:

    标签: gwt validation bean-validation


    【解决方案1】:

    只需使用@Editor.Ignore 注释您的wrapped 文本框。

    或者,您可以删除 implements IsEditor&lt;LeafValueEditor&lt;String&gt;&gt; 并使用 @Path("") 注释 wrapped 字段(您必须使用 null 值进行测试,但如果您可能会遇到它们,因为我不确定会正常工作)。

    或者您可以选择实现自己的LeafValueEditor&lt;String&gt;,而不是依赖TextBox 中的那个。

    【讨论】:

    • 谢谢。这可以工作,或者至少可以编译/生成,但是 editorDriver 始终不会报告错误,而在“手动”使用验证器时,会正确报告违规行为。有什么想法吗?
    • 我为 FormField 类实现了 HasEditorErrors 并将 @Editor.Ignore 添加到包装的 TextBox 中。
    • 我相信您“手动”使用验证器(没有与编辑器框架 AFAIK 的“集成”)并在EditorDriver 上调用setConstraintViolations
    • 好的,我明白了。 javadoc 似乎暗示这是自动的,请参阅“boolean hasErrors” => 指示最后一次调用 {@link #flush()} 是否导致任何错误。但是对 flush() 的调用根本不会影响错误。
    • 这些是编辑器自己产生的错误(EditorDelegate 上的reportErrorIntegerBox 会产生一个这样的错误,例如,如果您在框中键入无法解析为整数的内容)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多