How to set selected values using sfWidgetFormSelectMany in Symfony1? - symfony1

I want to use the sfWidgetFormSelectMany inside a sfForm. I have no problems setting the choices for the select, but how do I set the selected values when loading the form?
This is my code for the sfWidgetFormSelectMany so far:
$choices = $this->getScopes();
$this->widgetSchema['application_scopes'] = new sfWidgetFormSelectMany(array(
'choices' => $choices
));
$this->validatorSchema['application_scopes'] = new sfValidatorChoiceMany(array(
'choices' => array_keys($choices)
));

So, I have to answer the question by myself. Sometimes the best way is to ask a question to find the answer ;-)
As I am inside my form class, I just can set the values like this:
$this->setDefault('application_scopes', array('this','that'));

Related

I'm having troubles getting drop-down-menus in MVC to return data

I am very new to MVC as a whole, and I was sent to create a web application that would give the user options then change the view based on these options. First I created a simple "select" html drop down menu but I am under the assumption that this won't work.
I can supply all the actual code I currently have, I just don't know what would be important to see and what would just be bothersome to wade through.
When I had created a constructor for the model, it wouldn't go through that constructor or else it would be easier.
Sorry since this all sounds probably bad, but any help would be amazing.
There are couple ways to do that , here's what i do :
create the function in your controller (The one that returns the list of items you want to include in the dropdownmenu) . let's name it getNames();
Then return that to the view using #viwbag.Names;
In the view you can declare a variable , something like:
#{
SelectListItem[] Names = ViewBag.Name;
}
and the dropdown menu code should be something like this:
#Html.DropDownList("Names", Names)
Finally, here is sample code for the controller:
IEnumerable<SelectListItem> Names =
(from names in YourDB.students.ToArray()
select new SelectListItem
{
Text = names.Name,
Value = names.Id
}).ToArray();
var Templist = names.ToList();
ViewBag.Roles = Templist.ToArray();

how to Rebind Telerik grid

Consider I have below mentioned Telrik Grid and first time it is loaded by employee details(name, Description). I want to add a row in telrik grid when i click add button which is out side of grid. My problem is how to add new record with existing record. I mean how to rebind the grid.
#(Html.Telerik().Grid<Project.Models.Employee>()
.Name("myName")
.DataKeys(keys => keys.Add(c => c.EmpId))
.Columns(columns => {
columns.Bound(o => o.Name).Width(200);
columns.Bound(o => o.Description).Width(400);
})
)
Please, provide me better solution. Remember my button out side the grid.
Take a look at the following example. This demo explains how to do the editing with the grid using AJAX.
http://demos.telerik.com/aspnet-mvc/grid/editingajax
Go through the demo - things are pretty clear what to do.
Lohith (Tech Evangelist, Telerik India)
If you in client side, then you can use javascript function rebind():
var grid = $("#Grid").data("tGrid");
//send additional arguments by passing them as a literal JavaScript object
grid.rebind({customerID : "ALFKI"});
But for more custom answer i need more information about your scenario. Is it ajax binding, server binding, or else. How do you add row (is it editing in cell or else) etc.
'It could be helpfull'
var grid = $("#Grid").data("tGrid");
//send additional arguments by passing them as a literal JavaScript object
grid.rebind({customerID : "SAJJAD"});

Getting all selected checkboxes from a FormCollection

I have a form which contains a whole bunch of checkboxes and some other types of control too. I need to retrieve the names of each selected checkbox.
What is the best way to do this? Can I do it with a linq query maybe?
So in pseudocode, I'm looking to do something like this:
var names = formCollection
.Where(c => c is Checkbox && c.Checked)
.Select(c => c.Name);
Update It seems the way MVC submits checkboxes is different from how a normal form would behave, as an hidden field is also rendered. I found the details here: How to handle checkboxes in ASP.NET MVC forms?
Anywho, I've got it working with the help of that thread and the answer from BuildStarted below. The following code did the trick.
var additionalItems = form.AllKeys
.Where(k => form[k].Contains("true") && k.StartsWith("addItem"))
.Select(k => k.Substring(7));
Unfortunately that type of information isn't available in the collection. However if you prepend all your checkboxes with something like <input type='checkbox' name='checkbox_somevalue' /> then you can run a query like
var names = formCollection.AllKeys.Where(c => c.StartsWith("checkbox"));
Since only the checked values will be posted back you don't need to validate that they're checked.
Here's one that grabs only checked values
var names = formCollection.AllKeys.Where(c => c.StartsWith("test") &&
formCollection.GetValue(c) != null &&
formCollection.GetValue(c).AttemptedValue == "1");
This is one of the old questions not active for years but I stumbled on it. My problem was that I have an array of check boxes - let's say the name is 'IsValid' and wanted to get the status of each of the check boxes (my project was in MVC 5). On form submit i did the loop of form collection and got the values as...
if (key.Contains("IsValid"))
sV = (string[])collection.GetValue(key.ToString()).RawValue;
Since on form post the hidden field value was also posted with the checked check boxes; the array contained one additional value of 'false' for ONLY checked check box. To get rid of those i used following function; I hope that it helps somebody and if my approach is wrong then a better solution would be helpful to me as well!
sV = FixCheckBoxValue(sV);
private string[] FixCheckBoxValue(string[] sV)
{
var iArrayList = new List<string>(sV);
for (int i = 0; i < iArrayList.Count; i++)
{
if (iArrayList[i].ToString() == "true")
{
iArrayList.RemoveAt(i + 1);
}
}
return iArrayList.ToArray();
}
Answering the post about the array of checkboxes, you could initially filter out the checkboxes by name.
var keys = formCollection.AllKeys.Where(x => x.StartsWith("IsValid");
Then you could just loop through them as a collection.

Help with asp.net mvc select dropdown

I have a marital status field in my users table that its just varchar
yet I only want to give the users four options (married, single, widowed and divorced) and i want to have the correct one selected when Im editing the form.. is it possible? please help.
This should point you in the right direction:
<%= Html.DropDownList("listName", new string[] { "Married", "Single", "Widowed", "Divorced" }
.Select(m => new SelectListItem(){
Selected = model.MaritalStatus == m,
Text = m,
Value = m
})); %>
Assuming that your model has a 'MaritalStatus' field,
Selected = model.MaritalStatus == m
will select the status of your model by default.
This blog post should lead you in the right direction:
http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx
you'd have to give us a little more information and maybe a code sample of what you have so far to get a more specific answer.

Html.ListBox

I am trying to re-select items in a listbox with asp.net mvc
Html.ListBox("SupplierId",
new SelectList(Model.Suppliers, "Id", "Name", Model.SelectedSuppliers))
Here is the viewdata
var viewData = new ViewData.SubstrateEditViewData(
new DataAccess.SubstrateRepository().GetItemById(id),
new DataAccess.SupplierRepository().GetItems(),
new DataAccess.SupplierSubstrateRepository().GetItems().Where(s => s.SubstrateId ==id).Select(s => s.Supplier));
for some reason its not selected the items even tho I can see the Model.SelectedSupplier containing two Supplier objects.
Thanks
Note what only ids of items should be passed to selectedValues parameter of MultiSelectList() method so you should use
Html.ListBox("SupplierId", new MultiSelectList(Model.Suppliers, "Id", "Name",
Model.SelectedSuppliers.Select(s => s.Id)))
The documentation for the SelectList constructor refers to a single value. It does't look like passing in a List or IEnumerable of values will result in a listbox with multiple values selected.
I struggled with the same problem a few weeks ago. The default extension methods for MultiSelect lists do not behave as expected. I ended up just looping through the items myself and setting their selected property manually.

Resources