java - Play 框架验证匹配字段

标签 java playframework

对于我所有的表单提交,我正在创建文档以接收提交的信息

例如 public static void formAction(@Valid FormDocument formDocument){ ...

我喜欢这个,因为它让我的 Controller 看起来整洁,并且更容易看到表单中请求的内容。

我的注册文件看起来(经过裁剪)如下:

@Email
@Required
public String email;
@Required
public String password;
@Required
public String confirmPassword;

我的问题是有没有一种方法可以检查密码是否与此类本身中的 confirmPassword 字段相匹配。目前我正在检查 Controller 并在失败时将验证消息传回。我认为如果在文档本身内完成会更整洁。

提前感谢您的任何反馈/回答!

最佳答案

来自Play documentation ,他们有您的确切用例。

Equals

Checks that the value is equal to another parameter’s value, using the value’s equals method, e.g. for checking for a password confirmation field.

Annotation syntax:

@Equals("passwordConfirmation") String password

所以,你只需要

@Email
@Required
public String email;
@Equals("confirmPassword") 
public String password;
@Required
public String confirmPassword;

关于java - Play 框架验证匹配字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806184/

相关文章:

Java:在Windows 上安装JDK 后应该设置哪些必要的环境变量,如何设置?

java - 区分循环中同名的局部变量

java - 使用 Play 进行用户管理!框架 2.0.3

junit - 作为 IntelliJ IDEA JUnit 运行配置运行 playframework 测试

java - 如何在elasticsearch中创建不同的文档但ID相同

java - Facebook Graph API 不显示生日

java - Springboot、Vaadin 和 RestController

java - Play Framework : parse "CompletionStage" (Java8) response

playframework - 如何在play2.0框架中创建自定义编译错误页面?

使用 Play 框架 2.3 时出现 java.lang.IllegalArgumentException