使用 Validator 验证表单
Validate form with Validator
在验证器组件上,我需要评估哪个文本字段出错,我找到了一个 Isvalid() 方法但对我的海豚无效,因为我不知道哪个值出错。
我怎么知道哪个约束和组件无效?,
boolean isValid(Component cmp)
没有public.
此致
我不确定您使用的是否正确Validator
,因为您没有提供任何代码。但是,下面的代码应该可以帮助您入门:
使用单个验证器进行验证:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
.
.
.
Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addConstraint(emailField, RegexConstraint.validEmail())
.addConstraint(passwordField, new LengthConstraint(6));
if (val.isValid()) {
System.out.println("All validated fields pass the constraints");
} else {
// show dialog here
}
使用单个验证器进行验证:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
.
.
.
Validator valEmail = new Validator();
valEmail.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valEmail.addConstraint(emailField, RegexConstraint.validEmail());
if (valEmail.isValid()) {
System.out.println("Email field passes the constraint");
} else {
// show dialog here
}
Validator valPassword = new Validator();
valPassword.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valPassword.addConstraint(passwordField, new LengthConstraint(6));
if (valPassword.isValid()) {
System.out.println("Password field passes the constraint");
} else {
// show dialog here
}
除上述之外,我建议使用 TextComponent,因为它会在每个组件下方无缝显示错误消息,而无需对话框。
作为奖励,您甚至可以在所有验证字段都有效之前禁用提交按钮:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
import com.codename1.ui.*;
.
.
.
TextComponent email = new TextComponent().constraint(TextArea.EMAILADDR)
.labelAndHint("Email").focusAnimation(false);
email.setName("email");
TextComponent password = new TextComponentPassword()
.labelAndHint("Password").focusAnimation(false);
password.setName("password");
Button submit = new Button("Submit");
Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addSubmitButtons(submit)
.addConstraint(email, RegexConstraint.validEmail())
.addConstraint(password, new LengthConstraint(6));
在验证器组件上,我需要评估哪个文本字段出错,我找到了一个 Isvalid() 方法但对我的海豚无效,因为我不知道哪个值出错。
我怎么知道哪个约束和组件无效?,
boolean isValid(Component cmp)
没有public.
此致
我不确定您使用的是否正确Validator
,因为您没有提供任何代码。但是,下面的代码应该可以帮助您入门:
使用单个验证器进行验证:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
.
.
.
Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addConstraint(emailField, RegexConstraint.validEmail())
.addConstraint(passwordField, new LengthConstraint(6));
if (val.isValid()) {
System.out.println("All validated fields pass the constraints");
} else {
// show dialog here
}
使用单个验证器进行验证:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
.
.
.
Validator valEmail = new Validator();
valEmail.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valEmail.addConstraint(emailField, RegexConstraint.validEmail());
if (valEmail.isValid()) {
System.out.println("Email field passes the constraint");
} else {
// show dialog here
}
Validator valPassword = new Validator();
valPassword.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valPassword.addConstraint(passwordField, new LengthConstraint(6));
if (valPassword.isValid()) {
System.out.println("Password field passes the constraint");
} else {
// show dialog here
}
除上述之外,我建议使用 TextComponent,因为它会在每个组件下方无缝显示错误消息,而无需对话框。
作为奖励,您甚至可以在所有验证字段都有效之前禁用提交按钮:
import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
import com.codename1.ui.*;
.
.
.
TextComponent email = new TextComponent().constraint(TextArea.EMAILADDR)
.labelAndHint("Email").focusAnimation(false);
email.setName("email");
TextComponent password = new TextComponentPassword()
.labelAndHint("Password").focusAnimation(false);
password.setName("password");
Button submit = new Button("Submit");
Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addSubmitButtons(submit)
.addConstraint(email, RegexConstraint.validEmail())
.addConstraint(password, new LengthConstraint(6));