Simple improvement to play.data.validation.Error
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
play framework |
Fix Committed
|
Undecided
|
Unassigned | ||
1.0 |
Won't Fix
|
Undecided
|
Unassigned | ||
1.1 |
Fix Committed
|
Undecided
|
Unassigned |
Bug Description
It would be nice if the fields on the Error class were public. That way the message field could be used as an error code, and the messages could also be customized more easily. This would be especially useful with custom validations.
// How it looks at the moment:
package play.data.
public class Error {
String message;
String key;
String[] variables;
...
}
Example of where it would be useful to have public fields:
/**
* Checks if a database field already exists
*/
abstract class CheckIfFieldExists extends Check {
private static final String KEY = "validation.
public boolean isSatisfied(Object provider, Object value) {
if(value == null) {
return true;
}
String fieldName = getFieldName();
Provider existing = Provider.
if (existing == null) {
return true;
}
if (existing.
return true;
}
// It would be nice to have a better way to do this as well
return false;
}
protected abstract String getFieldName();
}
// Somewhere else in the code
validation.
List<Error> errors = validation.
for(Error error : errors) {
if(
// The provider's email already exists. Redirect him to the login page
}
}
Changed in play: | |
status: | New → Fix Committed |
This is still not quite right - the problem is that when the Error object is converted to JSON, the message is translated, so the error code is lost.
For example if the error was description
message: validation.maxSize
key: provider.
variables ["1024"]
Then when the Error object is created it will translate message into something like "The provider. description is more than 1024 chars". description is more than 1024 chars", description
So the resulting JSON will have the translated message:
{
message: "The provider.
key: provider.
variables ["1024"]
}
It would be nice to preserve the original error code as well, so that the client can take a different action depending on the type of error: maxSize" , description is more than 1024 chars", description
{
code: "validation.
message: "The provider.
key: provider.
variables ["1024"]
}