1

Tried this on play 2.1.1 Have two fields: picture (input file) , description (input text), like this:

@(pictureForm: Form[FileUpload]) 

@import helper._

@form(action = routes.Uploader.uploadSimple, 'enctype -> "multipart/form-data") {


  @helper.inputFile(pictureForm("picture"))

  @helper.inputText(pictureForm("description"))

  <button type="submit" > submit </button>

}

Marked description as @required in the fileupload-pojo. If the validation fails ( User gave no description) and the bad-request is thrown, the input-file selection, that the User selected before, vanishes.

My Controller works like this:

public static Result upload() {

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

    MultipartFormData body = request().body().asMultipartFormData();
    Map<String, String[]> result = body.asFormUrlEncoded();

    FilePart picture = body.getFile("picture");

    if (picture != null) {
        formData.put("contentType", picture.getContentType());
        File file = picture.getFile();
        formData.put("picture", file.toString());
    }

    formData.put("description",result.get("description")[0]);               

    Form<FileUpload> uploadForm = Form.form(FileUpload.class).bind(formData);

    if (uploadForm.hasErrors()) {
        return badRequest(upload.render(uploadForm));
    } else {
        return ok("Got picture uploaded");
    }
}

I don't want the user to fill out the picture-selection because of one error of another field for usability purposes. The validation occurs in the FileUpload-class because i wanted it to be separated:

package models;

import java.util.ArrayList;
import java.util.List;

import play.data.validation.ValidationError;

public class FileUpload {

public String contentType;
public String description;
public String picture;

public List<ValidationError> validate() {

    List<ValidationError> valErrors = new ArrayList<ValidationError>();

    if (description == null || description.isEmpty())
        valErrors.add(new ValidationError("description",
                "Give a description."));

    if (contentType != null && !contentType.isEmpty()) {
        if (!(contentType.equals("image/jpeg") || contentType.equals("image/png"))) 
            valErrors.add(new ValidationError("picture",
                            "Wrong format."));
    }   else {
            valErrors
            .add(new ValidationError("picture",
                    "No picture chosen."));
        }

    return valErrors.isEmpty() ? null : valErrors;

}

}

I need to get the path of the selection in the input file. How do i define FileUpload to connect it with the input-file field? My controller takes the multipart-data and i can get the file, but i throw a badrequest with the validations in case of an error.

I hope this kind of makes sense.

1 Answer 1

0

Here's one way of solving it (roughly):

Add an optional path to your form:

public class FileUpload {
   public String path;
   //...
}

Now when you parse the form, you add the path of the uploaded file in your form:

FilePart picture = body.getFile("picture");

if (picture != null) {
    //...
    formData.put("path", picture.getRelativePath());
}

To make sure this path is send back to the server between requests, you could put it into a hidden input:

@form(action = routes.Uploader.uploadSimple, 'enctype -> "multipart/form-data") {
    @*...*@
    <input type="hidden" name="path" value="@pictureForm.data.get("path")" />
}

Finally, when validating the form, instead of checking if there's an uploaded picture you must now also include a case where the picture is already uploaded and the path of the uploaded picture is in path.

To increase usability you could also hide the file input when there's a path specified in pictureForm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.