0
votes

Suppose we have following entities (representing a m:n relation, with additional column on the join table):

public class User {
    private String name;
    private List<Login> logins;
}

public class Login {
   private User user;
   private Website website;
   private String login;
}

public class Website {
   private String name;
   private List<Login> logins;
}

I want to create a User edit form that contains one login input field per each existing website (so that all existing websites are in the form). E.g., having 2 websites defined (website1, website2), I would like to see:

form

My problem is with achieving following behavior on submission of the form: if login input field is filled for a website, it should be added to user1's logins, and if it's empty, it should not be added/get removed.

I created the form using User model (for user name), and website's fields use ListView backed by a model of all logins (taken straight from DB). This makes my form look as expected, but the behaviour is not there, as websites model is independent from the User model. What is your recommended approach?

2
one login for multiple website right?if it so then you have to change model approach? - soorapadman
No, that's one login per website. - marvin82
for me your model seems to be ok. if you have any issue with coding share it. will sorted out - soorapadman

2 Answers

1
votes

Use a ListView backed by a list of all possible Login objects, ie the existing Login object if it exists and a dummy/empty/new Login object for each Website for which it doesn't exist.

Then on form submission save those Login objects which have a not-null and not-empty login field.

You could create a bean instead of directly using the Login object but it would work the same way.

If you also want to delete Login objects for which the user removed the login value, create some way in which you can check the Login object has been saved before (ie, its login field was notempty once) and delete the object if it is empty now.

0
votes

Changing ListView to PropertyListView did the trick. Model gets updated properly and therefore I can do any required postprocessing in onSubmit(). With the ListView, form was rendered fine, but changes in login input fields were ignored.