Beispiel #1
0
 /**
  * Binds Json data to this form - that is, handles form submission.
  *
  * @param data data to submit
  * @return a copy of this form filled with the new data
  */
 public Form<T> bind(com.fasterxml.jackson.databind.JsonNode data, String... allowedFields) {
   return bind(
       play.libs.Scala.asJava(
           play.api.data.FormUtils.fromJson(
               "", play.api.libs.json.Json.parse(play.libs.Json.stringify(data)))),
       allowedFields);
 }
Beispiel #2
0
 /** 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);
 }
Beispiel #3
0
 /** 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<>();
   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) {
         if (messagesApi != null && lang != null) {
           messages.add(
               messagesApi.get(
                   lang, error.messages(), translateMsgArg(error.arguments(), messagesApi, lang)));
         } else {
           messages.add(error.message());
         }
       }
       allMessages.put(key, messages);
     }
   }
   return play.libs.Json.toJson(allMessages);
 }
Beispiel #4
0
  protected Map<String, String> requestData(Http.Request request) {

    Map<String, String[]> urlFormEncoded = new HashMap<>();
    if (request.body().asFormUrlEncoded() != null) {
      urlFormEncoded = request.body().asFormUrlEncoded();
    }

    Map<String, String[]> multipartFormData = new HashMap<>();
    if (request.body().asMultipartFormData() != null) {
      multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded();
    }

    Map<String, String> jsonData = new HashMap<>();
    if (request.body().asJson() != null) {
      jsonData =
          play.libs.Scala.asJava(
              play.api.data.FormUtils.fromJson(
                  "",
                  play.api.libs.json.Json.parse(
                      play.libs.Json.stringify(request.body().asJson()))));
    }

    Map<String, String[]> queryString = request.queryString();

    Map<String, String> data = new HashMap<>();

    for (String key : urlFormEncoded.keySet()) {
      String[] values = urlFormEncoded.get(key);
      if (key.endsWith("[]")) {
        String k = key.substring(0, key.length() - 2);
        for (int i = 0; i < values.length; i++) {
          data.put(k + "[" + i + "]", values[i]);
        }
      } else {
        if (values.length > 0) {
          data.put(key, values[0]);
        }
      }
    }

    for (String key : multipartFormData.keySet()) {
      String[] values = multipartFormData.get(key);
      if (key.endsWith("[]")) {
        String k = key.substring(0, key.length() - 2);
        for (int i = 0; i < values.length; i++) {
          data.put(k + "[" + i + "]", values[i]);
        }
      } else {
        if (values.length > 0) {
          data.put(key, values[0]);
        }
      }
    }

    for (String key : jsonData.keySet()) {
      data.put(key, jsonData.get(key));
    }

    for (String key : queryString.keySet()) {
      String[] values = queryString.get(key);
      if (key.endsWith("[]")) {
        String k = key.substring(0, key.length() - 2);
        for (int i = 0; i < values.length; i++) {
          data.put(k + "[" + i + "]", values[i]);
        }
      } else {
        if (values.length > 0) {
          data.put(key, values[0]);
        }
      }
    }

    return data;
  }