nicohvi
nicohvi

Reputation: 2270

Adding additional information when rendering a form in Play! 2.0.2

I'm adding more information to a user model through a LinkedIn API, and all this code is handled in a controller. When the information is added to the model, I re-render the form (of User class) in a new view. Is there any way to attach additional information to this form (built into Play!), to display which fields have been updated by the API-call?

Use case:

  1. User enters view/edit profile page

  2. User clicks "import information from LinkedIn"

  3. An API-call is made to the LinkedIn API - the user is then returned to a specific controller (called Social) which handles the JSON to JAVA conversion of data, and binds the new data to the user model, and stores the updated model in the database.

  4. The controller then redirects the user to the index() controller which renders the view/edit profile view again. (now I'd like to add the functionality that the user can see which of the fields on his profile were modified due to the API-call).

For instance, something similar to form.errrors() (called form.info() or something). If this isn't possible, then is it any way to gain access to the FieldElements, and maybe add something to them?

Cheers!

Upvotes: 3

Views: 569

Answers (1)

ndeverge
ndeverge

Reputation: 21564

I don't clearly see the problem.

When the controller redirects the user to its profile page, you just have to populate the form with the data from the database.

Something like that:

public static Result profile(String userId) {
    Form<User> userForm = form(User.class);

    User user = User.findById(userId); // data have been set in the database
    if (user != null) {
         userForm = userForm.fill(user);
    }

    return redirect(your.view.render(userForm));
}

Upvotes: 2

Related Questions