I have been using play 1.2.5rc4 for development of one app and I have found an interesting behaviour of Play. Look at the following code:
User user = new User(some attributes...);
boolean userSaved = user.validateAndSave();
During the call to new User I add a validation error using validation.addError() method. Then I would expect that during the validateAndSave() it returns false, as the validation already contains an error. But it doesn’t. I am wondering whether this is a bug, or feature. I mean, can anyone think of some use case when this behaviour would be useful?
Another question is, why is validateAndSave() not the default save() method? I would expect that if I call user.save() it wouldn’t save a user which violates the constraints, but apparently it does (I have an example, where a user violating the @Unique constraint gets inserted into db). Any idea why this can be useful? Wouldn’t it be better to have method save() behaving as validateAndSave() and then a method e.g. saveWithoutValidation()? I can see the current naming of the methods causing a lot of problems.
Validate and save does not look at the existing validation object, but instead validates the specific object that you are asking to be validated.
Your second question is because you may have some validation, which is specific to one of your screen designs, but maybe on an admin screen, you want to have different business rules. Forcing objects to be correct at all times is a restriction to the developer that should not be forced by the framework.