我正在使用vaadin创建一个Java项目。现在我有一个用户注册表格:
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个字符)。我想知道的是:有没有简单的内置方法呢?我到过这里: https://vaadin.com/api/com/vaadin/data/Validator.html
但我不明白如何使用验证器,或者即使这是我想要使用的。我一直在谷歌寻找用法示例,但到目前为止没有成功。谢谢你的帮助!
答案 0 :(得分:14)
以下内容适用于Vaadin 7.在Vaadin 8中已删除了validate()方法。
Vaadin中的所有字段类型都实现了Validatable
接口,该接口具有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上显式调用validate
(如果它是缓冲字段,也可以使用commit()
)方法实例触发验证。如果值无效,validate
将抛出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());
}
}
Validators实现Validator接口,Vaadin附带了几个有用的验证器。有关这些文档的更多信息,请查看API文档:https://vaadin.com/api/7.4.5/com/vaadin/data/Validator.html
自定义验证器易于实现,以下是Book of Vaadin:
中的示例class MyValidator implements Validator {
@Override
public void validate(Object value)
throws InvalidValueException {
if (!(value instanceof String &&
((String)value).equals("hello")))
throw new InvalidValueException("You're impolite");
}
}
final TextField field = new TextField("Say hello");
field.addValidator(new MyValidator());
field.setImmediate(true);
layout.addComponent(field);
答案 1 :(得分:4)
轻松使用Vaadin 8 com.vaadin.data.Binder
即可验证您的字段。请参阅手册中的Binding Data to Forms。
创建df = df.rename(columns = df.iloc[0].to_dict()).iloc[1:]
和活页夹以验证文本字段。
df = df.reset_index(drop=True)
来自binder的调用validate()将调用验证操作。
TextField
验证提交的内容。您可以在页面的任何位置调用此方法。我曾经在价值变化或点击按钮时调用它。
答案 2 :(得分:3)
问题解决了, 显然我以前看不够深。它来了:
field.addValidator(new StringLengthValidator("The name must be 1-10 letters (was {0})",1, 10, true));
这里的所有细节: https://vaadin.com/book/-/page/components.fields.html