如何使用 Scala 验证 Vaadin 中表中的字段

发布于 2024-12-10 12:20:55 字数 973 浏览 0 评论 0原文

如何验证 vaadin 表中的字段?例如带有正则表达式的年份字段:

   val persons: BeanContainer[Int, Person] =
   new BeanContainer[Int, Person] classOf[Person])

   persons.setBeanIdProperty("id")
   persons.addBean(new Person("Thomas", "Mann", 1929, 123123))
   persons.addBean(new Person("W. B.", "Yeats", 1923, 643454))
   persons.addBean(new Person("Günter", "Grass", 1999, 743523))

   // create table
   val table: Table = new Table("Nobel Prize for Literature", persons)

   table.setVisibleColumns(Array("id", "firstName", "lastName", "year"))

   table.setColumnHeader("lastName", "last name")
   table.setColumnHeader("firstName", "first name")
   table.setColumnHeader("year", "year")

   // create a validator 
   val yearValidator = new RegexpValidator("[1-2][0-9]{3}", 
                                       "year must be a number 1000-2999.");

   // TODO check the year field!
   table.addValidator(yearValidator)

我创建了一个正则表达式验证器,但是如何将验证器设置到正确的字段?

How can I validate a field in a vaadin table? For example the year field with a regex:

   val persons: BeanContainer[Int, Person] =
   new BeanContainer[Int, Person] classOf[Person])

   persons.setBeanIdProperty("id")
   persons.addBean(new Person("Thomas", "Mann", 1929, 123123))
   persons.addBean(new Person("W. B.", "Yeats", 1923, 643454))
   persons.addBean(new Person("Günter", "Grass", 1999, 743523))

   // create table
   val table: Table = new Table("Nobel Prize for Literature", persons)

   table.setVisibleColumns(Array("id", "firstName", "lastName", "year"))

   table.setColumnHeader("lastName", "last name")
   table.setColumnHeader("firstName", "first name")
   table.setColumnHeader("year", "year")

   // create a validator 
   val yearValidator = new RegexpValidator("[1-2][0-9]{3}", 
                                       "year must be a number 1000-2999.");

   // TODO check the year field!
   table.addValidator(yearValidator)

I create a Regex Validator, but how can I set the validator to the right field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-12-17 12:20:55

您必须使用字段工厂拦截字段的创建,并在其中添加验证器:

    table.setTableFieldFactory(new DefaultFieldFactory() {
        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            Field field = super.createField(item, propertyId, uiContext);
            if ("year".equals(propertyId)) {
                field.addValidator(new RegexpValidator("[1-2][0-9]{3}", 
                                   "year must be a number 1000-2999.");
            }
            return field;
        }
    });

(Java,不是 Scala,但将其转换为 scala 应该很简单)。

You have to intercept the creation of the fields with a field factory and add the validators there:

    table.setTableFieldFactory(new DefaultFieldFactory() {
        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            Field field = super.createField(item, propertyId, uiContext);
            if ("year".equals(propertyId)) {
                field.addValidator(new RegexpValidator("[1-2][0-9]{3}", 
                                   "year must be a number 1000-2999.");
            }
            return field;
        }
    });

(Java, not Scala, but it should be straightforward to translate this to scala).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文