【问题标题】:GXT GridInlineEditing field validationGXT GridInlineEditing 字段验证
【发布时间】:2014-07-23 15:40:40
【问题描述】:

我正在使用 GWT/GXT 和 gwt 编辑器框架构建一个编辑器应用程序。在某些情况下,我必须编辑日期列表。为此,我选择使用GridInlineEditing,它工作正常,但我还必须在gridInlineEditing 中对我的DateField 进行一些格式验证。

基本上,编辑的默认行为是在CompleteEditEvent 被触发时“记录”更改,无论验证结果如何。因此,我尝试像这样覆盖onCompleteEditHandler 方法(这显然是做到这一点的唯一方法according to GXT forum):

public class NameValueDTMEditorWidget extends GenericEditableListView<DTM, Date> implements Editor<NameValueDTM> {
private final static DTMProperties props = GWT.create(DTMProperties.class);

ListStoreEditor<DTM> values;
@Ignore
private DateField df = new DateField();

public NameValueDTMEditorWidget(String widgetTitle) {
    super(widgetTitle, new ListStore<DTM>(props.key()), props.dtm());

    DateTimeFormat dtf = DateTimeFormat.getFormat("yyyyMMddHHmmss");

    df.setPropertyEditor(new DateTimePropertyEditor(dtf));

    addEditorConfig(df); // parent class method basically doing: editing.addEditor(df), editing is GridInlineEditing

    // Modifying grid cell render for a Date
    Cell c = new DateCell(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));
    getColumnModel().getColumn(0).setCell(c);

    values = new ListStoreEditor<DTM>(getStore());

    editing.addCompleteEditHandler(new CompleteEditEvent.CompleteEditHandler<DTM>() {
        @Override
        public void onCompleteEdit(CompleteEditEvent<DTM> event) {
            df.validate(); // I force field validation
            if (df.getValue() == null || !df.isValid()) { // if value's not valid
                getStore().clear(); // clear the store
                DTM e = GWT.create(DTM.class);
                getStore().add(e); // add a new value
                editing.startEditing(event.getEditCell()); // start editing new value
                df.forceInvalid(); // force invalid to get invilid display on the field
            }
        }
    });
}

它几乎可以满足我的要求,当值无效时它会保持在版本中,但是当我在错误的值之后输入一个有效值时,它知道该值是有效的,但它不会退出编辑模式。我也尝试做同样的事情来保持错误的输入值,而不是清除我的存储并创建一个新值,它的行为方式完全相同,除了使用这种方法我也有显示问题。

有人知道怎么做吗?我对String 的列表也有同样的问题。

【问题讨论】:

    标签: java validation gwt gxt inline-editing


    【解决方案1】:

    尝试使用转换器

        editing.addEditor(columnConfig, new Converter<String, Date>() {
    
            @Override
            public String convertFieldValue(Date date) { /* called when you leave the cell */
                GridCell cell = (GridCell) editing.getActiveCell();
                ListStore<DTM> store = grid.getStore();
                DTM dtm = store.get(cell.getRow());
    
                /*
                 * here you have the dtm object belongs to related cell and the date as input.
                 * done with your validation here.
                 */
    
                return dtf.format(date);
            }
    
            @Override
            public Date convertModelValue(String date) { /* called when you focus in the cell */
                return dtf.parse(date);
            }
    
        }, df);
    

    【讨论】:

    • 我现在已经切换到另一个项目,但一旦我回到这个项目,我会告诉你它是否有效。
    【解决方案2】:

    在验证字段之前尝试调用Field#clearInvalid(我认为这将在您的示例中的df.validate(); 行之前完成)。

    【讨论】:

    • 我仍然有完全相同的行为。我猜这个问题更多地与 GridInlineEditing 相关,而不是与字段或字段验证本身有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多