Wicket form with list of all available values - wicket

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:
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?

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.

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.

Related

Yii2 adding parameters to the model with a function

I have two types of data. Articles and article types. They are stored a postgres DB. Every article instance should have particular type. All articles have common properties like "name", "create_date" and so on that are in every article and fields that are specific for the specific type like "some_image", "ingredients", and so on for the type1 and "images" and "points" for type2.
The additional properties for the specific type are stored in a single json column in the db table.
When the url "some_path/articles/create/2" is opened a form for creating an article of type 2 is displayed
I have an Articles ActiveRecord model and ArticlesController.
My problem is that in the model I have only have the common properties. And the additional properties are always different.
I need a method in the model or in the controller with which I can add the additional properties with the proper rules and all (like the common properties in the model) and then in the view I can render all fields without difference.
Is there a way this to be done and how?
I read how this can be done with Javascript, but I want it to be dobe before page rendering, not after that
May be this can help.
Use Scenario in your model
Model class has scenario function which you can inherit.
User Register Scenario - Required - username, password, email
User Login Scenario - Required - username, email
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['login'] = ['name','password'];//Scenario Values Only Accepted
return $scenarios;
}
and you can use it as

Generate custom Azure web form

I am working on Azure C# project. There is webrole with web form collecting customer’s information.
Depending on the information, they submit I need to create new custom web form for each one of them.
How can I generate new web form with custom web controls inside same site with code? I would prefer not create different site for each customer.
Any ideas appreciated!
You don't have to create a custom web form for each customer, you can have it all in on dynamic form and draw the input controls according to the customer.
You will have to have a form a form or a database table to configure the input needed for each customer which will be used in drawing the form.
ex:
You can have a table named: CustomerAttibute with the following columns
Customer ID
Attribute Name
Attribute Type (Number, text, boolean, ...)
Is Required
Validation Reg Expression
In the web form, you will read the customer ID and retrieve the attributes related to that customer, and use these attributes to draw the form, ex: an attribute of type text will be rendered as a textbox but an attribute of type boolean will be rendered as a checkbox
Capture the values from the user and insert it into another table, ex: CustomerAttributeValue which will have the attribute ID and attribute value, the value will be string to accommodate for any type

Multi page form data validation in Play Framework?

I am a beginner in java play framework. I an building an app which will take form data spread in multiple pages say 4 to 5 pages. But all the data is mapped to same model. How can I get the data uploaded by user per page, validate against my model's constraints, and at the end save whole data in my model.
For ex:- If page 1 has name field which is required, and page 2 has hobbies field which is required. Then how can i specifically validate data filled in that particular page, navigate till last page, and save all the data in model, in last page.
Model would have 60-70 fields.
I am using Hibernate ORM.
Thanks !
You could prefill the next form with the values of the last form, save them in the database, go to the next form, load the entered values from the values and pre-fill the next form and so on. For this, use a method in your controller:
public static Result fillOutForm1(){
Hobby form1 = new Hobby("sitting still", "diabetes");
Form<Hobby > preFilledForm = newHobbyForm.fill(form1);
return ok(views.html.YOURVIEWCLASS.render(preFilledForm);
}
With this you "send" some values to the first view class with a form. In there, the user answers some more fields and hits Submit.
So you need a route back to your controller, to handle the new input in routes.conf:
GET /Form1 controllers.Application.fillOutForm1()
POST /Form1 controllers.Application.sendForm1()
This is in your controller class:
public static Result sendForm1(){
Form<Hobby> boundHobby = newHobbyForm.bindFromRequest();
Hobby newHobby = boundHobby.get();
//In your hobby class you need a finder implementation, so that you can interact with the database. With save() you put it into the database. This may be different for the database you use!
newHobby.save();
return redirect(routes.Application.index());
}
You can then not redirect to the index, but to the next view class with the next form and there get your last answers from the db, prefill them again and put them into the form. Do this as often as you need.

JPA: User-Entity contains another User-Entity

In my domain model i have a user entity, which contains another user (composite pattern). When i want to persist the user, i get an error which says something like "Data too long for column...". This happens because the contained user is tried to be stored as a BLOB. I want the ID of the contained user to be stored - not the whole object as a BLOB.
What kind of annotation must i user in the case? The cardinality is 1 : 0..1 - so a user can contain another user.
here is the code ...
#Entity
#Table(name="flex_app_user")
public class User implements Serializable {
private User client;
public User(){
}
#OneToOne
public User getClient() {
return client;
}
}
... and the problem resulted by a change in the datamodel. The correct annotation is OneToOne. And everything works fine.
kind regards,
Jochen
The annotation must be #OneToOne.
As you are using the composite pattern can you use (fetch=FetchType.LAZY) on the relation ship used?

cakephp passing a variable to another controller

I am hoping to get pointed in the right direction. I want to pass a variable from one controller into another controller.
what I want to do is have a person register a business then they are taken to a form to register a user. a business is a different controller/table to a user however the user requires the id/primary key of the business as a foreign key in the user table. How would I go about changing controllers and carrying the foreign key over?
the primary key for the business table is an autogenerated/autoincremented int in the database
i am unsure on how I would approach this but have a feeling it is to do with session data?
Why dont you pass the id in the url?. I'd do it like this:
Display /business/add. This is the form used to create a "business".
After saving the business in your controller, redirect to /business/add_user/123 (where "123" is the id of your business). This page displays and saves the users. Since you have passed the business_id in the url you'd have to add it as a foreign key manually into the $this->request->data before saving the user.
Of course that inside the controller of /business/add_user/123 you should verify a few thing: check if the business_id was passed as parameter in the url, check if the business exist, maybe check that the connected user was the one that created the businnes, etc
Hope this helps