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.
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Date with SimpleDateFormat in Java
I'm doing this workaround in my controllers as i don't know a better solution:
As you can imagine, the isNew value checks your creation case, in my case weather a seq - or 0 in case of creation - is passed via the url
The method userExists(userName: String): Boolean does the check for uniquness.
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 sameForm
object for both creation and edition in real world programs.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: