@Repeat Form Assistant with Complex Object - Play Framework

I am unable to repeat the field with the selected value according to the data that I have in my model.

I currently have a User model:

@Required
public String username;

@ManyToMany
public List<Stay> places;

And my stay model:

@Required
@ManyToOne
public Place place;

@Required
@Formats.DateTime(pattern="yyyy-MM-dd")
public Date startDate;

@Required
@Formats.DateTime(pattern="yyyy-MM-dd")
public Date endDate;

My Scala form:

    @(formUser: Form[User])
...
    @repeat(formUser("places"), min = 1) { stayField =>
      <div id="groupLocationField">
        // Don't manage to reach User->Places->Place
        @select(formUser("stayField.places"), options(Place.optionsTitle))
        @inputDate(formUser("stayField.startDate"))
        @inputDate(formUser("stayField.endDate"))
      </div>
    }

stayField is a form. Type of field that contains Field(null,places[1],ArrayBuffer(),None,ArrayBuffer(),Some(Paris Mon Oct 29 00:00:00 CET 2018 Wed Jun 11 00:00:00 CEST 2014 null))

But it looks like I turned my Place and Dates into a string. @stayField contains the value of the myString method for my Stay.

@stayField.value = Paris Mon Oct 29 00:00:00 CET 2018 Wed Jun 11 00:00:00 CEST 2014
@stayField.value.productArity = 1
+4
source share
2 answers

Found a solution:

 @repeat(formUser("places"), min = 1) { stayField =>
      <div id="groupLocationField">
        @select(formUser(stayField.name.toString + ".place"), options(Place.optionsTitle))
        @inputDate(formUser(stayField.name.toString + ".startDate"))
        @inputDate(formUser(stayField.name.toString + ".endDate"))
      </div>
    }

stayField.name will show places [1], places [2], etc.

, Place.optionsTitle Place.options, @select.

    public static Map<String,String> optionsTitle() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Place c: Place.find.orderBy("title desc").findList()) {
            options.put(c.title, c.title);
        }
        return options;
    }   
    public static Map<String,String> options() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Place c: Place.find.orderBy("title desc").findList()) {
            options.put(c.id.toString(), c.title);
        }
        return options;
    }

EDIT: Title(), :

@select(formUser(stayField.name.toString + ".place"), options(Place.optionsTitle))

:

@select(formUser(stayField.name.toString + ".place.id"), options(Place.options))
+7

"" , , , .

, User :

:

def edit(id: Long) = authorizedAction(Administrator) {
  Ok(views.html.user.edit(id, editUserForm.fill(User.findById(id).get)))
}

:

@(id: Long, userForm: Form[User])

@main("Edit User") {
  <h2>Edit User</h2>
  @helper.form(action = routes.UserController.update(id)) {
    <fieldset>
      <input type="hidden" name="id" value="@userForm("id").value" />
      @helper.inputText(userForm("name"), '_label -> "Name")
      @helper.inputText(userForm("email"), '_label -> "Email Address")
      @helper.select(userForm("permission"), 
          models.Permission.displayValues, 
          '_label -> "Permissions", '_default -> "-- Select --",
          '_showConstraints -> false)
    </fieldset>
    // Submit button
  }
}

"" "" "userForm", , HTML "".

+1

Source: https://habr.com/ru/post/1536326/


All Articles