Simple improvement to play.data.validation.Error

Bug #526721 reported by dirk
6
This bug affects 1 person
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.validation;
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.exists";

    public boolean isSatisfied(Object provider, Object value) {
        if(value == null) {
            return true;
        }

        String fieldName = getFieldName();
        Provider existing = Provider.find(fieldName + " = ?", value).first();
        if (existing == null) {
            return true;
        }

        if (existing.getEntityId() == ((Provider) provider).getEntityId()) {
            return true;
        }

        // It would be nice to have a better way to do this as well
        setMessage(KEY);
        return false;
    }

    protected abstract String getFieldName();
}

// Somewhere else in the code
validation.valid(myObject);
List<Error> errors = validation.errorsMap().get("myField");
for(Error error : errors) {
    if(error.message == CheckIfFieldExists.KEY) {
        // The provider's email already exists. Redirect him to the login page
        flash.error(Messages.get("Provider.validation."+error.message, error.variables))
        providerLogin();
    }
}

Changed in play:
status: New → Fix Committed
Revision history for this message
dirk (australiandeveloper) wrote :

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
message: validation.maxSize
key: provider.description
variables ["1024"]

Then when the Error object is created it will translate message into something like "The provider.description is more than 1024 chars".
So the resulting JSON will have the translated message:
{
  message: "The provider.description is more than 1024 chars",
  key: provider.description
  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:
{
  code: "validation.maxSize",
  message: "The provider.description is more than 1024 chars",
  key: provider.description
  variables ["1024"]
}

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.