/** * Returns the form errors serialized as Json using the given Lang. * @param lang the language to use. * @return the JSON node containing the errors. */ public JsonNode errorsAsJson(Lang lang) { Map<String, List<String>> allMessages = new HashMap<>(); errors.forEach(error -> { if (error != null) { final List<String> messages = new ArrayList<>(); if (messagesApi != null && lang != null) { final List<String> reversedMessages = new ArrayList<>(error.messages()); Collections.reverse(reversedMessages); messages.add(messagesApi.get(lang, reversedMessages, translateMsgArg(error.arguments(), messagesApi, lang))); } else { messages.add(error.message()); } allMessages.put(error.key(), messages); } }); return play.libs.Json.toJson(allMessages); }
/** * Adds an error to this form. * * @param key the error key * @param error the error message * @param args the errot arguments */ public void reject(String key, String error, List<Object> args) { reject(new ValidationError(key, error, args)); }
/** * Formats the Play error message somehow, with the translation to the first available locale. * As hash arguments, it defines the field key as "field" and all other arguments as its index, e.g. "0", "1", "2". * @param locales current given locales * @param error Play's validation error * @return the error message localized and formatted */ default String format(final List<Locale> locales, final ValidationError error) { final Map<String, Object> args = new HashMap<>(); if (error.key() != null && !error.key().isEmpty()) { args.put("field", error.key()); } IntStream.range(0, error.arguments().size()) .forEach(index -> args.put(String.valueOf(index), error.arguments().get(index))); return format(locales, error.message(), args); } }
@BodyParser.Of(play.mvc.BodyParser.Json.class) public static Result editClientJSON() { Logger.debug("Reached editClientJSON"); Form<Client> clientForm = Form.form(Client.class).bindFromRequest(); if(clientForm.hasErrors()) {//check out for form errors for (String errorKey : clientForm.errors().keySet()) { for (ValidationError error : clientForm.errors().get(errorKey)) { Logger.error(error.key() + " = " + error.message()); } } return badRequest(); } //There is no error on the form so it is now safe to get the Client Client client = clientForm.get(); client.update(); Logger.debug("Client updated: " + client.name); response().setHeader(LOCATION, routes.ClientCtrl.getClientJSON(client.id).url()); return ok(); }
/** * Returns the form errors serialized as Json using the given Lang. */ public org.codehaus.jackson.JsonNode errorsAsJson(play.i18n.Lang lang) { Map<String, List<String>> allMessages = new HashMap<String, List<String>>(); for (String key : errors.keySet()) { List<ValidationError> errs = errors.get(key); if (errs != null && !errs.isEmpty()) { List<String> messages = new ArrayList<String>(); for (ValidationError error : errs) { messages.add(play.i18n.Messages.get(lang, error.message(), error.arguments())); } allMessages.put(key, messages); } } return play.libs.Json.toJson(allMessages); }
@Override public DynamicForm withError(final ValidationError error) { final Form<Dynamic> form = super.withError(new ValidationError(asDynamicKey(error.key()), error.messages(), error.arguments())); return new DynamicForm(super.rawData(), super.files(), form.errors(), form.value(), this.messagesApi, this.formatters, this.validatorFactory, this.config, lang().orElse(null)); }
errors.put(key, new ArrayList<ValidationError>()); errors.get(key).add(new ValidationError(key, error.isBindingFailure() ? "error.invalid" : error.getDefaultMessage(), arguments)); if(globalError instanceof String) { errors.put("", new ArrayList<ValidationError>()); errors.get("").add(new ValidationError("", (String)globalError, new ArrayList())); } else if(globalError instanceof List) { for (ValidationError error : (List<ValidationError>) globalError) { List<ValidationError> errorsForKey = errors.get(error.key()); if (errorsForKey == null) { errors.put(error.key(), errorsForKey = new ArrayList<ValidationError>());
/** * Returns the form errors serialized as Json using the given Lang. */ public com.fasterxml.jackson.databind.JsonNode errorsAsJson(play.i18n.Lang lang) { Map<String, List<String>> allMessages = new HashMap<String, List<String>>(); for (String key : errors.keySet()) { List<ValidationError> errs = errors.get(key); if (errs != null && !errs.isEmpty()) { List<String> messages = new ArrayList<String>(); for (ValidationError error : errs) { messages.add(play.i18n.Messages.get(lang, error.messages(), error.arguments())); } allMessages.put(key, messages); } } return play.libs.Json.toJson(allMessages); }
final ValidationError error = (ValidationError) dynamicPayload; result.rejectValue( error.key(), violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), error.arguments() != null ? error.arguments().toArray() : new Object[0], error.message() ); } else if (dynamicPayload instanceof List) { ((List<ValidationError>) dynamicPayload).forEach(error -> result.rejectValue( error.key(), violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), error.arguments() != null ? error.arguments().toArray() : new Object[0], error.message()
@Override public DynamicForm withError(final ValidationError error) { final Form<Dynamic> form = super.withError(new ValidationError(asDynamicKey(error.key()), error.messages(), error.arguments())); return new DynamicForm(super.rawData(), super.files(), form.errors(), form.value(), this.messagesApi, this.formatters, this.validatorFactory, this.config, lang().orElse(null)); }
builder.add( code.replace("typeMismatch", "error.invalid") ); validationError = new ValidationError(key, builder.build(), convertErrorArguments(error.getArguments())); } else { validationError = new ValidationError(key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); globalErrors.add(new ValidationError("", error.getDefaultMessage(), convertErrorArguments(error.getArguments()))); if (globalError instanceof String) { errors.put("", new ArrayList<ValidationError>()); errors.get("").add(new ValidationError("", (String)globalError, new ArrayList())); } else if (globalError instanceof List) { for (ValidationError error : (List<ValidationError>) globalError) { List<ValidationError> errorsForKey = errors.get(error.key()); if (errorsForKey == null) { errors.put(error.key(), errorsForKey = new ArrayList<ValidationError>());
/** * Returns the form errors serialized as Json using the given Lang. * @param lang the language to use. * @return the JSON node containing the errors. */ public JsonNode errorsAsJson(Lang lang) { Map<String, List<String>> allMessages = new HashMap<>(); errors.forEach(error -> { if (error != null) { final List<String> messages = new ArrayList<>(); if (messagesApi != null && lang != null) { final List<String> reversedMessages = new ArrayList<>(error.messages()); Collections.reverse(reversedMessages); messages.add(messagesApi.get(lang, reversedMessages, translateMsgArg(error.arguments(), messagesApi, lang))); } else { messages.add(error.message()); } allMessages.put(error.key(), messages); } }); return play.libs.Json.toJson(allMessages); }
final ValidationError error = (ValidationError) dynamicPayload; result.rejectValue( error.key(), violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), error.arguments() != null ? error.arguments().toArray() : new Object[0], error.message() ); } else if (dynamicPayload instanceof List) { ((List<ValidationError>) dynamicPayload).forEach(error -> result.rejectValue( error.key(), violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), error.arguments() != null ? error.arguments().toArray() : new Object[0], error.message()
@Override public DynamicForm withError(final ValidationError error) { final Form<Dynamic> form = super.withError(new ValidationError(asDynamicKey(error.key()), error.messages(), error.arguments())); return new DynamicForm(super.rawData(), super.files(), form.errors(), form.value(), this.messagesApi, this.formatters, this.validatorFactory, this.config, lang().orElse(null)); }
/** * @param key the field name associated with the error. * @return All errors for this key. */ public List<ValidationError> errors(String key) { if(key == null) { return Collections.emptyList(); } return Collections.unmodifiableList(errors.stream().filter(error -> error.key().equals(key)).collect(Collectors.toList())); }
/** * Adds an error to this form. * * @param key the error key * @param error the error message * @param args the error arguments */ public void reject(String key, String error, List<Object> args) { reject(new ValidationError(key, error, args)); }
/** * Returns the form errors serialized as Json using the given Lang. * @param lang the language to use. * @return the JSON node containing the errors. */ public JsonNode errorsAsJson(Lang lang) { Map<String, List<String>> allMessages = new HashMap<>(); errors.forEach(error -> { if (error != null) { final List<String> messages = new ArrayList<>(); if (messagesApi != null && lang != null) { final List<String> reversedMessages = new ArrayList<>(error.messages()); Collections.reverse(reversedMessages); messages.add(messagesApi.get(lang, reversedMessages, translateMsgArg(error.arguments(), messagesApi, lang))); } else { messages.add(error.message()); } allMessages.put(error.key(), messages); } }); return play.libs.Json.toJson(allMessages); }