Basic validation should be clear for you now. It is well-documented in the official documentation and shows you how to use the different annotations such as @Min
, @Max
, @Url
, @Email
, @InFuture
, @InPast
, or @Range
. You should go a step forward and add custom validation. An often needed requirement is to create some unique string used as identifier. The standard way to go is to create a UUID and use it. However, validation of the UUID should be pretty automatic and you want to be sure to have a valid UUID in your models.
You can find the source code of this example in the chapter2/annotation-validation
directory.
As common practice is to develop your application in a test driven way, we will write an appropriate test as first code in this recipe. In case you need more information about writing and using tests in Play, you should read http://www.playframework.org/documentation/1.2/test.
This is the test that should work:
public class UuidTest extends...