Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Scala在Vaadin中验证表中的字段_Scala_Validation_Vaadin - Fatal编程技术网

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

如何使用Scala在Vaadin中验证表中的字段,scala,validation,vaadin,Scala,Validation,Vaadin,如何验证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

如何验证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)

我创建了一个Regex验证器,但如何将验证器设置为正确的字段?

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

    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应该很简单)。

这里有更多关于验证表字段的信息:当然,很抱歉延迟。顺便说一句,我还有一个关于瓦丁的问题,也许你能再次帮助我。谢谢