Java 如何验证vaadin生成表单中的字段
我正在和瓦丁一起做一个Java项目。现在,我有一个用户注册表,如下所示:Java 如何验证vaadin生成表单中的字段,java,forms,validation,vaadin,vaadin7,Java,Forms,Validation,Vaadin,Vaadin7,我正在和瓦丁一起做一个Java项目。现在,我有一个用户注册表,如下所示: public class RegistrationComponent extends CustomComponent implements View { public static final String VIEW_NAME = "Registration"; public RegistrationComponent(){ Panel panel = new Panel("Registr
public class RegistrationComponent extends CustomComponent implements View {
public static final String VIEW_NAME = "Registration";
public RegistrationComponent(){
Panel panel = new Panel("Registration Form");
panel.setSizeUndefined();
FormLayout content = new FormLayout();
CheckBox checkBox1, checkBox2, checkBox3;
checkBox1 = new CheckBox("Check Box 1");
checkBox2 = new CheckBox("Check Box 2");
checkBox3 = new CheckBox("Check Box 3");
checkBox1.setRequired(true);
checkBox2.setRequired(true);
TextField mailTextField = new TextField("Email Address");
TextField passwordTextField = new TextField("Password");
TextField confirmPasswordTextField = new TextField("Confirm Password");
final Button submitButton = new Button("Submit");
content.addComponent(mailTextField);
content.addComponent(passwordTextField);
content.addComponent(confirmPasswordTextField);
content.addComponent(checkBox1);
content.addComponent(checkBox2);
content.addComponent(checkBox3);
content.addComponent(submitButton);
content.setSizeUndefined(); // Shrink to fit
content.setMargin(true);
panel.setContent(content);
setCompositionRoot(panel);
//listeners:
submitButton.addClickListener(new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
//
}
});
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent event){
//
}
}
当然,表单除了显示之外,什么都不做。
我想做的是,如果某些要求没有满足,让Vaadin在字段旁边显示错误消息。要求本身并不重要(假设我希望电子邮件字段至少包含8个字符)。我想知道的是:有什么简单的内置方法可以做到这一点吗?我在这里:
但我不知道如何使用验证器,甚至不知道这是否是我想要使用的。我一直在谷歌搜索使用示例,但迄今为止没有成功。谢谢你的帮助 问题解决了,
显然,我以前看得不够深。来了:
field.addValidator(new StringLengthValidator("The name must be 1-10 letters (was {0})",1, 10, true));
详情如下:
Vaadin 7
以下内容适用于Vaadin 7。validate()方法已在Vaadin 8中删除。
Vaadin中的所有字段类型都实现了validable
接口,该接口具有addValidator
方法,该方法接受Validator的实现作为参数
因此,要添加检查TextField值长度的验证器,您可以执行以下操作:
TextField textField = new TextField();
textField.addValidator(
new StringLengthValidator(
"Must be between 2 and 10 characters in length", 2, 10, false));
Vaadin字段具有向用户显示验证错误的内置功能。默认情况下,该字段将以红色突出显示,并在该字段旁边显示感叹号,将鼠标悬停在该字段上,将向用户显示更详细的消息
自动验证
默认情况下,该字段现在将在下一个服务器请求中进行验证,该服务器请求包含该字段到服务器的更改值。如果该字段设置为“立即”,则当该字段失去焦点时会发生这种情况。如果该字段不是立即的,则当其他UI操作触发返回服务器的请求时,将进行验证
显式验证
有时,您可能希望对何时进行验证以及何时向用户显示验证错误进行更多控制。通过将validationVisible
设置为false,可以禁用自动验证
textField.setValidationVisible(false);
准备验证字段时(例如,在按钮单击侦听器中),可以显式调用TextField实例上的验证
(如果是缓冲字段,还可以使用提交()
)方法来触发验证<如果值无效,代码>验证将抛出一个InvalidValueException
。如果要使用TextField组件中包含的验证错误的内置显示,还必须将validationVisible
设置回true
try {
textField.validate();
} catch (Validator.InvalidValueException ex) {
textField.setValidationVisible(true);
Notification.show("Invalid value!");
}
请注意,一旦validationVisbible设置回true,验证将隐式进行,因此如果要保持对验证的显式控制,则必须记住在下一个请求中将其设置回false
验证消息
可以从Validator.InvalidValueException实例中提取单个验证消息,该实例在调用validate()
或commit()
时抛出
try {
textField.validate();
} catch (Validator.InvalidValueException ex) {
for (Validator.InvalidValueException cause: ex.getCauses()) {
System.err.println(cause.getMessage());
}
}
验证器
验证器实现验证器接口,Vaadin附带了几个有用的验证器。查看API文档以了解有关以下方面的更多信息:
自定义验证器易于实现,下面是以下示例:
瓦丁8
使用Vaadin 8,您可以轻松验证字段。请参阅手册中的
创建TextField
和活页夹以验证文本字段
public class MyPage extends VerticalLayout{
TextField investorCode = new TextField();
Binder<MyBean> beanBinder = new Binder<MyBean>();
//Info : MyBean class contains getter and setter to store values of textField.
public MyPage (){
investorCode.addValueChangeListener(e->valueChange(e));
addComponent(investorCode);
bindToBean();
}
private void bindToBean() {
beanBinder.forField(investorCode)
.asRequired("Field cannot be empty")
.withValidator(investorCode -> investorCode.length() > 0,"Code shold be atleast 1 character long").bind(MyBean::getInvestorCode,MyBean::setInvestorCode);
}
//rest of the code .....
private void valueChange(ValueChangeEvent<String> e) {
beanBinder.validate();
}
}
以验证该文件。你可以在页面的任何地方调用它。我过去常在值更改或单击按钮时调用此选项。不幸的是,这在Vaadin 8中不再适用。Vaadin 8不再有.validate()方法…@StephaneGrenier,见Arundev。
public class MyPage extends VerticalLayout{
TextField investorCode = new TextField();
Binder<MyBean> beanBinder = new Binder<MyBean>();
//Info : MyBean class contains getter and setter to store values of textField.
public MyPage (){
investorCode.addValueChangeListener(e->valueChange(e));
addComponent(investorCode);
bindToBean();
}
private void bindToBean() {
beanBinder.forField(investorCode)
.asRequired("Field cannot be empty")
.withValidator(investorCode -> investorCode.length() > 0,"Code shold be atleast 1 character long").bind(MyBean::getInvestorCode,MyBean::setInvestorCode);
}
//rest of the code .....
private void valueChange(ValueChangeEvent<String> e) {
beanBinder.validate();
}
}
beanBinder.validate();