Symfony2 form dynamic select entity - ajax

I searched for a while, but i didn't find anything.
I want to create a dynamic select with another select. Example :
in my form i have two field (category & subcategory) linked to an entity, when i select an item in category drop-down the item in subcategory will change.
So ok i found i can manage with a event listener.
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$form = $event->getForm();
$data = $event->getData();
var_dump($data->getCategory());
});
Ok, i can get the selected category and after load the subcategory, but how i can call this with ajax ?
Hope you have any example...

The response is not so simple... Basically, if you want to build a form according to initial & submitted value, you need to attach an event listener to the PRE_SET_DATA & PRE_SUBMIT event. When the form is initialized, the PRE_SET_DATA event is fired. Here, you will receive your object as data in the form event or null if you don't provide one (Don't forget to handle this case). With this first part, you will be able to build your form dynamically according to your initial data.
Then, when you will handle your form, the PRE_SUBMIT event will be fired if the form is submitted. In this event, you will reveive a flat array wilt all the submitted datas. According to this array, you can rebuild your form according to the submitted datas. The main issue here is the array is an array & not your model object. Meaning you will not have a category object but his view representation (his ID)...
Hope it helps!

Related

Populate Select2 multiple select box with user selected data after Laravel 4 redirect

I have a Select2 multiple select box on a form, for users to select the countries they have previously visited. I'm using Laravel 4 and when the form is submitted, it then validates all form fields and redirects back to the form if any validation errors have occurred - repopulating the form with the users previous input ( using Input::old('???') ).
My questions is how can I reapply the users previous selections to the Select2 multiple select box, using the data stored in Input::old('countries[]') ?
Where in my JavaScript code would I add the ID's of the users previous selections?
$(document).ready(function() {
$('#inputCountries').select2(); // How do I add selections?
});
I think you just need to provide the array of IDs of countries as an argument of selected items. Might need to use one of array_keys to get just the IDs passed into the selected items array.

binding collection using Model binding query

I have a list of object as model. I have properties like Server, Database, User.
What i want is a dropdown with list of object bound to it and on the change of dropdown items, I have to populate the Database and User information in 2 different labels.
How can i achieve that using Model Binding ??
I can't understand what do you mean when asking about 'using Model Binding'. But it can be done with jquery/javascript. You create dropdownlist on page and on change event send selected item to server and receive 'Database and User information'. Example using Ajax helper: using ajax with dropdownlist mvc3

Joomla 1.7 custom toolbar button usage

I've added a custom button into my component's backend toolbar with this code:
JToolBarHelper::custom('libri.details','details.png','details.png','TOOLBAR_DETAILS',true);
This button needs that the admin checks one of the entries listed in the table.
You can see a screenshot here if you haven't understood what I'm talking about
The button is the one called "Dettagli" (no image at the moment).
I'm having some problems:
How do I append the checked entry's id to the address generated from the button?
I've put a details() method into the controller, it calls an instance of the model and a method inside it. The model's method returns to the controller the result of a query. How do I say to the controller to pass that result to a view called libro?
All information in the page will be sent through POST to the Joomla controller (in this case, libri). In your controller, you can use JRequest::getVar('varNameHere'); to retrieve it.
$this->setRedirect($urlOfView, $someStatusMessage)

Selected Dropdown value changes when validation fails-ASP.NET MVC

I am using MVC 3 with ASP.NET. I have a dropdown box and getting it populated from database. I am using validation on the View. If it fails the validation, I am displaying the same view with errors being caught in ViewDate.ModelState.AddModelError.
I am checking for the ViewData.Modelstate.IsValid property if true then execute the code else display the errors in the view.
It is diplaying the errors in the page, but the selected value in the drop down is getting reset when validation fails.
How do I make sure the seleceted drop down does not change when validation fails?
In the action that handles the form submission and validation, make sure you set the properties on your model object from the form before rendering the form view.
For example, in this question you can see how the Dinner object parameter in the Create action is reused when the View() is returned.
Set a breakpoint in the action handling the submission and check the property for the list of values. If it's null or empty reload it.
If your dropdownlist is being populated via javascript then it is possible that the property holding the list of values is empty on submission. This is common when using cascading dropdownlists such is the case with loading Province / State lists based off country. All cascading lists loaded after the model has been passed to the view must be reloaded using the selected value of for each dropdownlist in the controller action that is handling the submission.

onselect in one combo it changes data dynamically in other combo?

Hello sir I am new to the jsp and ajax world. my problem is
If i select one combo option then it should change the other combo options dynamically without submit button press.
for example if i select the country then it should shows their states in other combo.
I am using servlet & JSP and MS-ACCESS as backend. please reply as soon as possible.
THANKING YOU....
You need:
A piece of JSP which returns a string which can be parsed in javascript to create a list of possible combobox items (value, name) to select in the SECOND combobox, based on a VALUE which can be selected in the FIRST combobox
A piece of javascript which receives the onChange event of the FIRST combobox and uses the then selected value to create a XMLHTTP-request to your JSP (at 1)
A piece of javascript which you can pass to the XMLHTTP-request onReadyStateChange event to execute, which reads/parses the parseable string (from 1) and fills the second COMBOBOX with those values
Good luck

Resources