Spring MVC form with Multiple rows of same type of fields, each row with a submit button, not able to edit a single row - spring-mvc-test

I have a form with multiple rows, each row having some editable fields which are mapped
to the property of the model class through form input path and a submit(edit) button.
Now, the problem is while submission, I am not able to dynamically append some id to the path to identify the submitted record and get its values in the controller.
This is required because every form input path is having same name in each row.
public class FormBean {
Integer record id;
Integer amt1;
Integer am2;
}
and form has multiple rows, each row is mapped to some properties of form bean like
<form:input path = amt1>
Is there any way to identify the submitted record(the editable fields) based on id and edit the values.
Can someone please help me..

I found this link to be great in learning about multi row form using SPring MVC..
http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
In your case I feel that you need a 'Amount' bean with id, amt1 and amt2 and a 'AmountForm' class which gets and sets a list/map of 'Amount' objects (call this list as amounts).
Then in your form page you would do:
<form:form action="..." modelAttribute="amountForm">
and to store each entry in its corresponding map you can use
<input name="amounts[0].amt1" value="1" />
or
<input name="amounts[your_record_number].your_property" value="your_value" />
Thanks and All the best

Related

Initializing JPA subentities when submitting JSF datatable

I've recently received a business requirement for my JSF app to edit rows of a datatable, in the table itself.
The problem: the table is backed by a list of JPA entities, but the editable fields belong to their -optional- associations. So, as an example of such an editable selectOneListbox field, if I do:
<h:selectOneListbox id="rca" value="#{mainEntity.associatedEntity.rca}" converter="omnifaces.SelectItemsConverter">
<f:selectItem itemValue="#{null}" itemLabel="" />
<f:selectItems value="#{rcaOptions}" var="rca" itemLabel="#{rca}" itemValue="#{rca}" />
</h:selectOneListbox>
I eventually receive Target Unreachable, 'associatedEntity' returned null when submitting the table, for those rows where there is no associated entity yet, even if I left the selectOneListbox unchanged for those (null option selected). The questions:
how/where/when can I elegantly instantiate new subentities when submitting the form, only for those rows the user acted on (ajax-less preferably), preventing the error shown above? Forcibly creating subentities in the model for all rows when first rendering the table is IMHO a flaw in design.
How can I programmatically ignore this selectOneListbox -or any other input field- from the entire JSF lifecycle, conditionally, just like if the field never existed in the request? I tried implementing several converters and validators but the validation phase is still trying to get the value reference, no matter what I try, throwing the error shown above

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

Symfony: get new ID in a form

I have an object displaying within a form, a hidden field being related to its PK (field id).
When I create a new object, the field has a null value. Submitting the form, the object is inserted into the DB and it now has an ID, but the field in the page still has a null value.
If a reload the page, now the ID is indeed set in the hidden field.
In my opinion, this is due to the form processing of Symfony: when a create an object, it creates a form, with this form valid the object is saved but the form still uses the data before it was saved.
The question is: how to get the auto-incremented key in the form up-to-date? Shouldn't the form only have a reference to the object? Can't the value be updated?
make sur that you call $entityManager->flush() method after insert and that you bind your form whene you have same data in your request object
$form->submit($request->request->get($form->getName()));
You should have an Entity assigned to your Form by FormFactory. Then Symfony will fill that Entity with submitted values. What's left is only persist the Entity and flush to database.
You can find steb-by-step form submission in Symfony Cookbook

Foreign entity in form into different kind of input

I have two entities: product and category (Symfony 2.3).
I want to create a form in which an user can choose a product by first selecting the category. A user selects the category by clicking on image, then I want to set the image's value into a hidden input, but I don't see how can I change a foreign entity choice list to a hidden input (http://symfony.com/doc/current/reference/forms/types/entity.html).
How can I accomplish this? (how to change form input to hidden)
If I set cascade validation to true, will it for example check if a category really exist. (To prevent putting products with non-existing category from malicious users) ?
Part 1
To do this you need to use a data transformer to do two things:
transform an entity into an identifier that is either a string or integer so a form can render it as a hidden field.
transform the string or integer identifier into the entity when the form is submitted so that the parent entity can be saved with the correct relationship
The symfony docs I linked to above (here too) actually walk though an entire example of using a data transformer with a form.
As a shameless plug (because I believe it is helpful) I have written a little tutorial on using a data transformer for a hidden field with an entity id: http://lrotherfield.com/blog/symfony2-forms-entity-as-hidden-field/
Part 2
If you are using the data transformer then you don't need to worry about malicious users. The data transformer will fail because it will not be able to reverse transform the category from the fake id. In my tutorial the transformer will throw a Symfony\Component\Form\Exception\TransformationFailedException exception.
You can also write a validator (potentially using a call back) if you wanted that checks that the submitted category is real if you want an error to show in the form. Doctrine wont allow you to persist a fake category relationship as the foreign key constraint will fail.

CakePHP - Automatically populate form fields from model

I thought the form helper did this already, but can the fields in my form automatically be populated with values from the model or do I have to set them all as template variables and set them all as the value of each field manually?
The form is for a user profile, so I just want it to put the user's names, email, etc in the correct form fields automatically.
for example: in controller $this->data = $this->Model->find('first',array(...));
in view:
$this->Form->create('Model');
$this->Form->input('field1');
...
$this->Form->end('Save');