Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jroper
Copy link
Member

@jroper jroper commented Mar 18, 2014

With play 2.2.2 and the project samples/java/forms, in the route http://localhost:9000/contacts if you try to click in INSERT without filling any field, the view is refreshed and it shows the corresponding "Required field" error with more PROFILES than the ones created.

The same sample run using play-2.2.1 do not have this problem.

I don't know if the issue is in the controller side with parsing List of objects, or in the view side, with the Form object.

@umbreak umbreak changed the title duplicated list entries in bindFromRequest [2.2.2-java] duplicated list entries in bindFromRequest Mar 17, 2014
@umbreak
Copy link
Author

umbreak commented Mar 17, 2014

The bug is related with the @repeat helper, modified for the play 2.2.2. Whatever was added there seems to have something wrong. With the previous @repeat helper it worked. Could you (@jroper) please, test what I explain in the main post, check it and correct it?

@umbreak umbreak changed the title [2.2.2-java] duplicated list entries in bindFromRequest [2.2.2-java] duplicated list entries view for Lists. @repeat helper is wrong Mar 17, 2014
jroper added a commit to jroper/playframework that referenced this pull request Mar 18, 2014
Fixes playframework#2499

The actual bug was that the the Java Field.indexes implementation was
not returning distinct indexes when the form had no value associated
with it (eg, if it failed validation), so if a field that was repeated
had multiple sub fields, the repeat hepler would render it once for
every sub field.

This only just exposed itself because the old repeat helper simply
rendered as many fields as the maximum index.  The new one however
renders fields equivalent to what indexes exist in the form.
@jroper jroper restored the 2499-java-repeat-fix branch March 18, 2014 04:06
@jroper jroper added this to the 2.2.3 milestone Mar 18, 2014
@jroper
Copy link
Member

jroper commented Mar 18, 2014

Fixed. See the attached commit for an explanation of the bug.

We'll fix this in 2.2.3. As a work around in the meantime, you can use this implementation of the repeat helper:

  object repeat2 {
    def apply(field: play.api.data.Field, min: Int = 1)(fieldRenderer: play.api.data.Field => Html): Seq[Html] = {
      val indexes = field.indexes.distinct.sorted match {
        case Nil => 0 until min
        case complete if complete.size >= min => complete
        case partial =>
          // We don't have enough elements, append indexes starting from the largest
          val start = partial.max + 1
          val needed = min - partial.size
          partial ++ (start until (start + needed))
      }

      indexes.map(i => fieldRenderer(field("[" + i + "]")))
    }
  }

@jroper jroper added the defect label Mar 18, 2014
@umbreak
Copy link
Author

umbreak commented Mar 18, 2014

Thanks for the fast solution. But I'm afraid now there's a problem with the workaround you provided, ordering the result. Using the same forms sample do as follows:

  1. Go to http://localhost:8080/contacts
  2. Fill the "Label" field with anything
  3. Click in "add another profile"
  4. Click "insert" submit button.

You will see how in the response the profile order is exchanged and lost.

(I am experiencing those problems in my own project, forms is just an easy sample how to explain the wrong behavior)

@richdougherty
Copy link
Member

That fix LGTM. @jroper, would you like me to merge it?

@jroper
Copy link
Member

jroper commented Mar 21, 2014

Ok, seems like we need some more tests (though it's very hard to know if your tests are hitting hashmap ordering issues).

Fixes playframework#2499

The actual bug was that the the Java Field.indexes implementation was
not returning distinct indexes when the form had no value associated
with it (eg, if it failed validation), so if a field that was repeated
had multiple sub fields, the repeat hepler would render it once for
every sub field.

Additionally, in that case, the ordering of the indexes was made
random by virtue of being calculated fdrom the keys of a HashMap, where
but the indexes should be returned in order.

This only just exposed itself because the old repeat helper simply
rendered as many fields as the maximum index.  The new one however
renders fields equivalent to what indexes exist in the form.

New tests test both that only one of each index appears, and that
indexes are always returned in order.
@jroper
Copy link
Member

jroper commented Mar 21, 2014

I've added new tests/modified existing tests to assert the ordering of the repeated fields, and fixed the bug. @richdougherty review again then merge? This should also be merged to 2.2.x since it's a regression.

@jroper
Copy link
Member

jroper commented Mar 21, 2014

@umbreak Actually I didn't noticed that you were reporting the issue with the work around, I thought you said the issue was in my fix (the same issue occurred in the fix, and that issue is now fixed). I've updated the work around above to fix it (very easy, simply called .sorted on the indexes).

@richdougherty
Copy link
Member

I'll merge today if I can get master to build properly.

@jroper
Copy link
Member

jroper commented Apr 3, 2014

Master is blue.

@benmccann
Copy link
Contributor

@richdougherty looks like it's building properly now. shows green for me

richdougherty added a commit that referenced this pull request Apr 9, 2014
[2.2.2-java] duplicated list entries view for Lists. @repeat helper is wrong
@richdougherty richdougherty merged commit 1efc0df into playframework:master Apr 9, 2014
benmccann added a commit to benmccann/playframework that referenced this pull request Apr 9, 2014
benmccann pushed a commit to benmccann/playframework that referenced this pull request Apr 9, 2014
Fixes playframework#2499

The actual bug was that the the Java Field.indexes implementation was
not returning distinct indexes when the form had no value associated
with it (eg, if it failed validation), so if a field that was repeated
had multiple sub fields, the repeat hepler would render it once for
every sub field.

Additionally, in that case, the ordering of the indexes was made
random by virtue of being calculated fdrom the keys of a HashMap, where
but the indexes should be returned in order.

This only just exposed itself because the old repeat helper simply
rendered as many fields as the maximum index.  The new one however
renders fields equivalent to what indexes exist in the form.

New tests test both that only one of each index appears, and that
indexes are always returned in order.
@jroper jroper deleted the 2499-java-repeat-fix branch September 23, 2014 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants