Symfony FormBuilder create multidimensional field name - symfony

It is possible to use the basic types of field to create a multidimensional array name?
For example:
<input type="text" name="my_type[translations][name][de]">
<input type="text" name="my_type[translations][name][fr]">

You can use ColectionType. But then you will probably need to change something about how you're using input names like here name="my_type[translations][name][fr]", maybe name="my_type[translations][nameFr]". Using it, your inputs will look like
<input type="text" name="my_type[translations][0][nameDe]">
<input type="text" name="my_type[translations][0][nameFr]">
<input type="text" name="my_type[translations][1][nameDe]">
<input type="text" name="my_type[translations][1][nameFr]">
Here I also presume that you have Translation entity which would have nameDe and nameFr properties.

Related

ASP.NET Core using two models in single form

I am using Tuple to pass two models inside the view like code given below.
#model Tuple<AdvanceSearchModel, List<SearchUserModel>>
<form role="search" method="post" action="/Public/AdvanceSearch">
<div class="form-group">
<label>Name</label>
<input name="FullNames" type="text" class="form-control" value=""/>
</div>
<div class="form-group">
<label>Product</label>
<input name="Products" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>Location:</label>
<input name="Location" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>State</label>
<input name="States" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>Country</label>
<input name="Countries" type="text" class="form-control" value=""/>
</div>
</form>
All the name attributes inside inputs are of AdvanceSearchModel. How do I use tag helper such as asp-for when passing multiple model to the views containing one or multiple forms? Also how do I retain values of the form after submitting the form in above scenario?
As you can see in the source code of InputTagHelper
You can see it creates the name attribute based on the (lambda) expression in html-tag:asp-for.
what you need
You need a form name tag like this SearchUserModel[0].Location
Where:
SearchUserModel is the property name on the model which is in the controller method you post to
[0] is the index in the list
Location is the property on the iten in the list the SearchUserModel instance
My suggestion
Not to do
Extend the InputTagHelper and add a prefix option (which adds a prefex to the name).
Use a view model Not a tuple!
Create a partial view that only takes SearchUserModel + a prefix (like an int for which row in the list it is for example: usermodel[1])
In your view loop over the list and call the partial.
result
#model SearchUserModel
<input asp-for="Location" my-prefix="ListItem[#Model.Id]" class="form-control" />
Better longterm option
Make a HTML template on how SearchUserModel part of the form should look.
Do ajax call to get the data or put the data as json in your view. (or take step 3 from what not to do)
Generate the form with well structured javascript.
On submit Instead of submitting the form, parse the from to json and send this as json ajax call.
Why do i say this? It is easier to debug if you get weird databindings in your controller.
That said, option 1 is perfectly fine but it might lead to problems later, as it is very static template, you wont be able to add or remove rows easily.
References for proper html name tags for lists:
http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
How does MVC 4 List Model Binding work?

Select a radio button functional test Symfony2

I'm making some functional tests with symfony2. I want to select a radio button in a basic form :
<form method="post" action="mylink">
<input id="position_51" type="radio" name="user_position" value="51">
<input id="position_52" type="radio" name="user_position" value="52">
<input id="position_54" type="radio" name="user_position" value="54">
<input id="position_57" type="radio" name="user_position" value="57">
<button id="bt_submit" type="submit">Submit</button>
</form>
So I select the form
$buttonFrom = $client->getCrawler()->selectButton('bt_submit');
$form = $buttonFrom->form();
Now, if I want to select radio with a specific ID, like "position_54" and tick it. How to do? In all examples I found, tick() seem to be used in the name attribute of the input... That not help me in a radio button case.
$form['user_position'] doesn't seem to be a array...
Thanks
As said in the symfony doc about testing, you can select an option or a radio this way :
$form['user_position']->select('51');
Here is the API for the ChoiceFormField.

Render enum as group of inline Bootstrap checkboxes

Bootstrap can show a group of inline checkboxes like this:
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label>
Now suppose that these checkboxes must come from an enum in my viewmodel. So for example I have HolidayModel.RoomPrefs which is an enum like this:
[Flags]
enum ReservationPreferences {
Room = 2,
Breakfast = 4,
Lunch = 8,
Dinner = 16
}
I want to render this enum as a group of inline checkboxes (i.e. the markup above).
I can do so manually, but I rather use a helper or editor template, or something else generic, because the HolidayModel viewmodel has other enums and so I want to reuse the code.
Does Razor have something out of the box to support this?
At this time, MVC has no "out-of-the-box" way to do this. You need to use a combination of custom model binding, and/or editor templates and/or something else.
I've removed enums from my viewmodel and replaced with a series of ugly bools. Does the job.

Symfony2 Form with Doctrine Entity type array

I have an entity (Group) that has a data array (UserDisplayNames) (user_id => DisplayName) stored in it (A user can have different display name for different groups).
I'm looking for the best way to do a symfony2 form (in Sonata AdminBundle) which allows the key (user_id and value to be edited).
E.G.
Key <input type="text" name="key[]" value="{key}">
Value <input type="text" name="value[]" value="{value}">
Key <input type="text" name="key[]" value="{key}">
Value <input type="text" name="value[]" value="{value}">

JSPX scriplet inside HTML input type textbox

I have the following in my jspx file:
<jsp:scriplet>
int myvar = 2;
</jsp:scriptlet>
How can I put variable myvar into a textbox (id=myinput) value using JSTL or scriptlet (I can do this using session variable)
<input type="text" id="myinput" value="...the value of myvar..."/>
Thanks
<input type="text" id="myinput" value="<%=myvar%>"/>
Just a little typo in previous answer. Please use like this
<input type="text" id="myinput" value="<%=myvar%>"/>

Resources