Question

I'm validating a form in Play framework(java), and it is adding commas to my form values after submission.

Here's the form:

#{form @doCreate()}
    <input type="text" name="session.sessionName" value="${flash['session.sessionName']}"/>
    <input type="text" name="session.jumpDate"value="${flash['session.jumpDate']}"/>
    <select id="jumpers" multiple="multiple" name="jumpers[]" title="Click to Select a Jumper">
        ..several options here
   </select>
#{/form}

Here's the form handler:

public static void doCreate(JumpSession session, String[] jumpers) {

    validation.required(session.getSessionName()).message("sessionName.required");
    validation.required(session.getJumpDate()).message("jumpDate.required");
    validation.required(jumpers).message("jumpers.required");


    if (validation.hasErrors()) {
        params.flash(); // add http parameters to the flash scope
        validation.keep(); // keep the errors for the next request
        create();
    }

    render();
}

The method create() renders the same form that was just displayed:

public static void create() {
    boolean isAuthorized = Security.isConnected();
    // Get the user details
    String userid = Security.connected();

    render(isAuthorized, userid);
}

The validation all works fine except the value in each field has a ',' character added to the end after validation. If I submit a completely blank form, it comes back with a comma(',') character in every field. If I enter a value in only one field, that value comes back with a comma pasted on the end. What's more, if I submit again, for every single comma before there are now 2 more commas!

EDIT: I've also tried using ${flash['session.jumpTime']?:""} to make sure the variable is actually defined.

EDIT: It's happening in the params.flash() method. System.out.println(params.allSimple()) reveals that there are no commas, but the values stored in flash after params.flash(), as revealed by System.out.println(flash), show commas. When I add the values to flash by hand, the commas never show up.

for (Map.Entry<String, String> entry : temp.entrySet())
        {
            flash.put(entry.getKey(), entry.getValue());
        }

This hack solves my issue, but I'd still like to know why I can't just use params.flash().

Was it helpful?

Solution

I've found the issue, and it had nothing to do with Play after all. A javascript library I was using (jQuery tools) was cloning the elements on the page without me knowing, thus creating double form submission. Since every form element was doubled, the values were separated by a comma. This was undetectable in the params variable, because it only inserted the comma in the .flash() method.

Pshaw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top