博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

JIRA now interrogates the InvalidInputException and puts field specific error messages onto the screen.

NOTE : Its up to the validator to ensure it gets the field names right for the current screen scheme.

For example here is example of a Validator that can put specific errors onto specific fields on the default screen.

public class EvilValidator implements Validator
{

    public void validate(Map transientVars, Map args, PropertySet ps) throws InvalidInputException
    {
        Issue issue = (Issue) transientVars.get("issue");

        Map errorMap = new HashMap();
        checkForEvil(issue, errorMap);

        if (errorMap.size() > 0)
        {
            InvalidInputException inputException = new InvalidInputException("Evil has been detected");
            for (Iterator iterator = errorMap.entrySet().iterator(); iterator.hasNext();)
            {
                Map.Entry entry = (Map.Entry) iterator.next();
                inputException.addError((String) entry.getKey(), (String) entry.getValue());
            }
            throw inputException;
        }
    }

    private void checkForEvil(Issue issue, Map errorMap)
    {
        String desc = issue.getDescription();
        if (isEvil(desc))
        {
            errorMap.put(IssueFieldConstants.DESCRIPTION, "Evil is incarnate in the description");
        }
        String env = issue.getEnvironment();
        if (isEvil(env))
        {
            errorMap.put(IssueFieldConstants.ENVIRONMENT, "Evil is incarnate in the environment");
        }
        String summary = issue.getSummary();
        if (isEvil(summary))
        {
            errorMap.put(IssueFieldConstants.SUMMARY, "Evil is incarnate in the summary");
        }
    }

    private boolean isEvil(final String input)
    {
        return input != null && input.indexOf("evil") != -1;
    }
}

 参照:https://jira.atlassian.com/browse/JRA-12886