仅在创建对象时,如何才能应用Play 2唯一性验证? [英] How can I apply a Play 2 uniqueness validation only when an object is being created?

查看:70
本文介绍了仅在创建对象时,如何才能应用Play 2唯一性验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在创建对象时,如何才能应用Play 2唯一性验证?我可以添加一个自定义的verifying子句,但是在编辑(而不是创建)现有对象时会失败.

How can I apply a Play 2 uniqueness validation only when an object is being created? I can add a custom verifying clause, but it would fail when editing (as opposed to creating) an existing objet.

推荐答案

如果您不对版本和创建执行相同的验证检查,也许这意味着您不应该对两者使用相同的Form对象?实际上,在少数几种特殊情况下,在实际程序中使用相同的Form对象进行创建和编辑是有意义的.

If you are not performing the same validation checks on edition and creation maybe it means that you should not use the same Form object for both? Actually, there are only a few particular cases where it makes sense to use the same Form object for both creation and edition in real world programs.

另一方面,如果您不想复制创建和版本通用的某些验证逻辑,则可以在两个Form对象中重用映射片段:

On the other hand, if you don’t want to duplicate some validation logic that is common to both creation and edition you can reuse a mapping fragment in your two Form objects:

val commonMapping = "common" -> tuple(
    "foo" -> number,
    "bar" -> nonEmptyText
)
val creationForm = Form(tuple(
    commonMapping,
    "baz" -> date
) verifying (/* creation specific constraint */))
val editionForm = Form(tuple(
    commonMapping,
    "bah" -> boolean
))

这篇关于仅在创建对象时,如何才能应用Play 2唯一性验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆