在Vaadin7中有一个addValidator函数,但在Vaadin8中不存在。
Vaadin 7示例:
TextField user = new TextField("User:");
user.setRequired(true);
user.setInputPrompt("Your username");
user.addValidator(new NullValidator("Username can't be empty", false));
user.setInvalidAllowed(false);
发布于 2017-02-27 17:32:47
我在这里找到了答案:Whats New
示例:
new Binder<Person>().forField(tf)
.withValidator(str -> str.length() == 4, "Must be 4 chars")
.withConverter(new StringToIntegerConverter("Must be Integer"))
.withValidator(integer -> integer.equals(2017), "Wrong date")
.bind(Person::getBirthYear, Person::setBirthYear);
发布于 2017-03-24 07:30:09
accepted Answer by Diego D显示正确。这段代码似乎摘自Vaadin公司Type safe validation before and after converters发布的这段非常简短(3分钟)但非常有用的视频。展示了新的Vaadin 8验证方法。我将添加一些注释,显示扩展的语法,并提供完整的示例代码来实现完整的应用程序。
验证器+绑定器
在Vaadin8中,一个很大的区别是验证器需要 。在过去,您可以将验证器附加到字段,但现在在Vaadin8中,您只能将验证器附加到绑定器。Vaadin团队认识到,对于一些简单的情况,这种对绑定器的要求可能会被证明是恼人的,但在大多数情况下,他们明智地预计需要验证的情况很可能也会进行绑定。我相信,这是一种非常合乎逻辑的反思。在另一个Vaadin公司的视频Webinar: What's new in Vaadin 8?中讨论过。
验证和转换器
我们定义了两个不同的验证器,一个在转换器转换用户的数据输入之前调用,另一个在转换后调用。因此,流畅风格的withValidator
和withConverter
方法调用的顺序是纠正这里的行为的关键。当然,beforeConversion
和afterConversion
对于验证器对象来说不是很好的名称,但这样做是为了明确在本演示中在转换器之前或之后运行的意图。
Lambda synax可选
一个验证器使用传统的Java代码风格覆盖方法。另一个验证器使用Lambda语法。观看视频并查看the Diego D Answer,了解使用单行Lambda参数进一步简化的代码。
package com.example.valapp;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Binder;
import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.data.ValueContext;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import javax.servlet.annotation.WebServlet;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme ( "mytheme" )
public class MyUI extends UI {
@Override
protected void init ( final VaadinRequest vaadinRequest ) {
final TextField tf = new TextField ( "Enter year of birth:" );
Validator<String> beforeConversion = new Validator < String > ( ) {
@Override
public ValidationResult apply ( String s, ValueContext valueContext ) {
if(s.length ()!= 4) {
return ValidationResult.error ( "Year must consist of 4 digits" );
} else {
return ValidationResult.ok () ;
}
}
} ;
Validator<Integer> afterConversion = Validator.from ( value -> value.equals ( 2017 ), "Wrong year." );
new Binder < Person > ( )
.forField ( tf )
.withValidator ( beforeConversion )
.withConverter ( new StringToIntegerConverter ( "Input must be Integer" ) )
.withValidator ( afterConversion )
.bind ( Person:: getYearOfBirth, Person:: setYearOfBirth );
Button button = new Button ( "Tell me" );
button.addClickListener ( event -> Notification.show("This is the caption", "This is the description", Notification.Type.HUMANIZED_MESSAGE) );
setContent ( new VerticalLayout ( tf , button ) );
}
@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}
发布于 2018-03-07 23:47:34
如果由于创建动态表单而没有活页夹怎么办?
Vaadin 8.1支持移除支持动态表单的字段的绑定器。如果您使一个字段不可见,则删除该字段的绑定器。当您使字段可见时,重新添加绑定器。
https://stackoverflow.com/questions/42491883
复制