How do I save multiple fieldsets using the play framework form tag? - forms

I have multiple fieldsets containing radiobuttons and an input box on a page. I was given this app to maintain and currently the data is saved by iterating over a Scope.Params parameter, picking out each value, using params.get() and passing them along to a method to save.
What is a good way to execute this? Will I always have to use a Scope.Params parameter? I know one can parameterize the values of the input fields of the form in the action that is called upon submit. How do I do it for a fieldset?
Thanks.

In controllers you can automatically get parameters if you have in your method signature.
Let's say you have a from createUser.html calls a POST method in your template. It is in UserController and createUser.
In form you have:
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
<input type="radio" name="gender" value="male" /> Male<br />
<input type="radio" name="gender" value="female" /> Female
so in you controller if you have a method like this:
pulic statid void createUser(String firstname, String lastname, int gender) {
User user = new User(firstname, lastname, gender);
...
}
So you don't necessarily need to get parameters separately.
EDIT -> In order to save multi-fields for same for you could use arrays like:
in form you could have: <input type="text" name="firstname[]" />
in controller:
pulic statid void createUser(String[] firstname ...) {
for (int i=0; i<firstname.length; i++) {
...
}
...
}

Related

Checkbox input remains false on form submit

I am trying to post a form and even though all the inputs map their value accordingly in the receiving controller-method , the bool-s of checkbox-es remain false.
What am i doing wrong ?
POCO
class MyPoco
{
public string User{get;set;}
public bool Require {get;set;}
}
Form
<form id="createForm" method="post" action="[some url]">
<input type="checkbox" id="require" name="Require" />
<input type="text" id="user" name="User"/>
</form>
Controller (method)
[HttpPost]
[Route("[some url]")]
public async Task<long> CreateAsync(MyPoco request)
{
}
Why in my case above does request.Require is always false.I have mapped them correctly (the name attribute).
The reason is that you forget to set value of checkbox:
<input type="checkbox" id="require" name="Require" value="true" />
If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]
If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.
I see you are missing asp-for attribute for your checkbox input. Instead of name="Require", use asp-for="Require" as follows:
#model MyPoco
<input asp-for="Require" type="checkbox" id="require" />
If you don't want to use asp-for attribute then you have to use jQuery as follows:
$(document).on('change','#require',function() {
if ($(this).is(':checked')) {
$(this).val(true)
} else {
$(this).val(false)
}
});

Submitting list of items with removed first one deletes all items

i have a model that has a list of sub items in it , something like this :
class MyObj {
public string Name {get;set;}
public List<mySubObject> Items {get;set;}
}
and
class mySubObject {
public string Name {get;set;}
public int Order {get;set;}
}
Now, when i render the list with a for loop and editorFor, the html i get is somethign like this :
<input type="text" name="Items[0].Name">
<input type="Number" name="Items[0].Order">
<input type="text" name="Items[1].Name">
<input type="number" name="Items[1].Order">
...
<input type="text" name="Items[9].Name">
<input type="number" name="Items[9].Order">
Now imagine remove the first element from the HTML via jQuery because i no longer want it in my list, and then save the list. The data that goes back is without the first [0] element
and all the elements from 1 to 9 go to the server but the model binding fails and it displays (on the server) that the list of items is null
What am i doing wrong ?
Is this a bug of the default model binder ?
What am i doing wrong ?
Leaving holes in the indices.
Is this a bug of the default model binder ?
No, it's by design. You shouldn't leave any holes in the indices.
One possibility is to recalculate the indices when you remove a row but you will have to write a ton of crap javascript for that.
I would recommend you using a different approach. Instead of using sequential indices use Guids. The approach is detailed in the following blog post by Phil Haack: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx (Non-Sequential indices section).
And Steven Sanderson illustrated a nice little helper in this post that would allow you to very easily achieve this: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
That you could use like this:
#using(Html.BeginCollectionItem("Items"))
{
for (var i = 0; i < Model.Items.Count; i++)
{
#Html.EditorFor(x => x.Name)
#Html.EditorFor(x => x.Order)
}
}
and it will generate the following:
<input type="hidden" name="Items.Index" value="F769E33C-4D07-4586-8D9C-63D386C641FA" />
<input type="text" name="Items[F769E33C-4D07-4586-8D9C-63D386C641FA].Name">
<input type="Number" name="Items[F769E33C-4D07-4586-8D9C-63D386C641FA].Order">
<input type="hidden" name="Items.Index" value="F4A4A9BB-4427-497E-BFD5-3CDE1F46095B" />
<input type="text" name="Items[F4A4A9BB-4427-497E-BFD5-3CDE1F46095B].Name">
<input type="number" name="Items[F4A4A9BB-4427-497E-BFD5-3CDE1F46095B].Order">
...
Now feel free to add/remove rows as much as you like on the client with javascript without fearing that it would break the model binding of your collection.

struts2 input iteration

Im using struts2 with freemarker template and i want to iterate over inputs and save them in search criteria.
In my action i have
private SearchCriteria criteria.
with method setCriteria and get criteria
public void setCriteria(SearchCriteria criteria) {
this.criteria= criteria;
}
public SearchCriteria getCriteria() {
return criteria;
}
In the criteria I can set list of objects ProductToSearch, with lets say id and label inside.
In my ftl file im iterating over the list of produtctsToSearch
<input id="test1" type="text" name="criteria.produtctToSearch[0].id" value="123" />
<input id="test2" type="text" name="criteria.produtctToSearch[0].label" value="testLabel" />
<input id="test1" type="text" name="criteria.produtctToSearch[1].id" value="123" />
<input id="test2" type="text" name="criteria.produtctToSearch[1].label" value="testLabel" />
but after looking at my criteria object, list of productsToSearch inside is empty.
Im calling my action via ajax with data: $("#form").serialize();
serialized values are:
criteria.produtctToSearch%5B1%5D.id=123&criteria.produtctToSearch%5B1%5D.label=testLabel&...
Can you tell me what am i missing?

Dynamically adding form elements with jQuery and Zend_Form

I have a form in which people shall be able to add the same portion of elements with a plus-button, so that something like this is produced:
<div id="person-1" class="person">
<input type="text" name="name-1" id="name-1" />
<input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
<input type="text" name="name-2" id="name-2" />
<input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
<input type="text" name="name-3" id="name-3" />
<input type="text" name="age-3" id="age-3" />
</div>
I already managed to write jquery-code that allows me to add the same elements once again with a new id (name-1, age-1, name-2, age-2, name-3, age-3, …).
Of course, Zend_Form does not know about name-2 and name-3, so it just drops them when the form contains an error and is displayed again. Neither can I access the value of name-2 with $form->getValue('name-2'). I have to go over raw $this->getRequest()->getPost().
Is there a better method I can use to combine Zend_Form and javascript-based added form elements (of same type like an hardcoded element).
Caveat: In the real problem, it’s select and not input. Found out this could make a difference (with ->setIsArray(true)), but using select would blow up the example code.
What you could do is create a subform container inside your main form and add an X amount of subforms to that container.
For example:
class My_Form extends Zend_Form
{
private $_numPersons = 1;
public function setNumPersons($numPersons)
{
$this->_numPersons = (int) $numPersons;
}
public function init()
{
$container = new Zend_Form_SubForm();
$this->addSubForm($container, 'persons');
for($index = 0; $index < $this->_numPersons; $index++) {
$personForm = new My_PersonForm();
$container->addSubForm($personForm, $index+1);
}
}
}
When rendered, the input fields will have names like persons[1][name]. Note the $index+1, Zend_Form does not allow a form to be named '0'.
Ofcourse, you should only use this method if the amount of person subforms is limited.
Another strategy would be to override the isValid method and use a single My_PersonForm form to validate all the person data.
Sidenote; the above code will only work when you define the numPersons as part of the options set, when creating the form instance. E.g.;
$form = new My_Form(array('numPersons' => 10));

Salesforce - Process variable number of form fields

I've been able to do this in PHP but it's not translating to Salesforce very well.
I have a form to input an Opportunity. It has an Account, a Contact, and a variable number of fields that will be used to create custom objects (For my purposes an Opportunity is a trip, and the custom objects are legs of that trip). The Salesforce controller needs to create a new Opportunity with the Account and Contact (that's the easy part) but then it needs to create a new custom object (Leg__c) for each leg of the trip.
My form looks like this:
<input type="text" name="Account" />
<input type="text" name="Contact" />
<div id="leg0">
<input type="text" name="dep[0]" />
<input type="text" name="arr[0]" />
</div>
<div id="leg1">
<input type="text" name="dep[1]" />
<input type="text" name="arr[1]" />
</div>
...
I'm not even sure where to begin on this one...
Assuming you know how many legs you need, you can simply create a list of them in your visualforce controller:
public list<Leg__c> liLegs {get; set;};
// upon oppty creation:
liLegs = new list<Leg__c>();
for (integer i = 0; i < iNumLegs; i++)
{
liLegs.add(new Leg__c());
}
Then you can just loop over these in your page like so:
<apex:repeat var="v" value="{!liLegs}">
<apex:inputField value="{!v.Dep__c}"/>
<apex:inputField value="{!v.Arr__c}"/>
</apex:repeat>
The input fields will correspond to the fields in each entry in the list, so then in your Save action or whatever you're using you can just insert the list insert liLegs;.
Hope this is of help and I haven't missed the mark, let me know if so! PS. I've just written this code directly in here so it may not be 100% syntactically correct ;)