In which Symfony event can I have access to which submit button was clicked? - symfony

I would like to do some transformation on the submitted data based on which button on my form was clicked. Here is the scenario:
I have a text field named chain which holds the command chain.
I have a command text field that holds the last command.
I have two buttons on the form: Send and Send chain.
When Send is clicked, chain is set to command, erasing whatever was in it before. When Send chain is clicked, however, the contents of command are added to the end of chain after a delimiter, effectively creating a chain of commands.
I need to be able to check to see which of these two buttons was clicked so that I can set chain to the appropriate value.
Why am I not doing this in the controller? The problem is that I need to modify the value of chain. Since I cannot modify the values of an already-submitted form, I need to do this in an event, I assume. As I mentioned above, I need chain to either be equal to command, or chain + [delimiter] + command.

If I understood you correctly, you don't need an event at all. There is a built in method isClicked() since #2.3. Example from documentation:
if ($form->isValid()) {
// ... do something
// the save_and_add button was clicked
if ($form->get('save_and_add')->isClicked()) {
// probably redirect to the add page again
}
// redirect to the show page for the just submitted item
}
Link to chapter.
Update
Normally, the code I showed can be used in event - FormEvents::POST_SUBMIT to be precise, but as you already pointed out, you can not modify form data after submission. To be honest I can't come up with perfectly working example, so I will share an idea hoping it will lead to something, or at least until someone else suggest something else.
How about you add a hidden non mapped field to your form. Then (assuming you are allowed to use javascript in your form) you can attach event listener on when form is submitted and based on which button was clicked you can populate that hidden field with specific value. After that you can add event listener to your FormType and read the value of this hidden field, using FormEvents::POST_SET_DATA.
I am sorry I can not be more helpful as of now. If I came up with something I will edit my post.

Here is what I needed to do:
I couldn't use the POST_submit event because I couldn't transform the data after submission.
The isClicked() of both buttons returned false in other events, because the form hadn't been submitted yet.
The solution is something like this:
$data = $event->getData();
if (isset($data['send']) {
// transform the values here
}
In other words, the only way to check if a particular button was clicked is to check to see if its name exists as an index in the array returned by $event->getData().

Related

Google App Maker Widget Validate on Datasource select

I have three different fields/textbox widgets, that rely on querying the same data source to be checked to avoid duplication. For reasons, I do not want to turn on the unique/required for those three fields.
I put some code to check for validation. My problem is that when I call the form's validate function, it takes some time till the validation comes back with an error message. However the form's validate returns immediately and allows people to click Submit.
How can I avoid this problem?
Block till validation kicks (setTimeout function?)
Set a separate invisible field such as working and set the validationError on the field and clear after validations clear? This will probably be a numeric field so that I can wait for all streams in parallel to finish.
Final question. is Validate a blocking function as it goes through the fields? I am guessing Yes.

google maps API autocomplete - how to handle direct entry

I have a google autocomplete input, and when the user selects one of the suggested addresses I have a place_changed function that splits the returned address into the fields necessary for a form, including lat & lng.
If instead the user types in an address and doesn't select one from the autocomplete list, then I need to geocode it to get the information. The place_changed function doesn't fire. So, I can put an onchange listener on the input which handles the direct user input.
The issue lies in that both the place_changed and onchange events fire when a suggestion is chosen from the autocomplete list.
Is there a way, in the onchange handler, to detect that the reason for it being called is the result of an autocomplete selection? it can then return without taking any further action.
Their is many way to change the address we can put string selection as a fire so when they throw the address Google goes to individual address not by the suggestion like the blue lines can Identifying the photographs not the back view so also needed some more photographer for move futher

How can I set the data into a template dynamically?

I have a list of Task objects.
I have a template which contains a modal form to edit a task.
When a user clicks the link to a single Task I want to set the task to be edited to the Task that was clicked and then render the form.
I'm trying it with reactive variables and Tracker.depend. But I haven't quite gotten it working yet. The data on Template.taskUpdateForm doesn't get set properly.
Please see this commit to see what I'm working on.
https://github.com/laran/eisenhower/commit/e89890d49f5b772849d09fd1f719a6cdafa58125
When you click on a task to edit, just store it's _id or the whole task in a Session variable, and in the helper function for your modal use that same session variable. No need to worry about Tracker depend. This always works for me.
I was setting the selectedTask to the wrong thing. I just needed SelectedTask.set(this.task) instead of SelectedTask.set(this)

adding a button in tryton view form

hi i have been searching and i cant find any tutorial about how to add a button in my view_form part of a custom module.
i wanted to add a button and make it call a method i made every time it is clicked.
in xml view form :
<label name="fieldstring"/>
<field name="fieldstring"/>
<button name="dosomething"/>
code:
def dosomething(cls,records):
#treatement
is there any example module that is using a button associated to a treatment??
In order to add a button to a view you have to make 3 steps:
Add the button to the _buttons dictionary of ModelView class. Normally this is done in the setup method of your class. Here you can define the icon and the states (when the button is invisible for example). If nothing needed you can define it with an empty dictionary.
For example:
#classmethod
def __setup__(cls):
super(Class, cls).__setup__()
cls._buttons.update({
'mybutton': {},
})
More complex examples can be found on tryton modules, for example:
http://hg.tryton.org/modules/account_invoice/file/84a41902ff5d/invoice.py#l224
Declare your method and decorate it with ModelView.button (in order to check access right to this button). For example:
#classmethod
#ModelView.button
def mybutton(cls, records)
#DO whatever you want with records
NOTE that the name method must be the one that you use as key of the _buttons dictionary in step1.
And finally add it to the view. You can find all the attributes that can be used on:
http://doc.tryton.org/3.2/trytond/doc/topics/views/index.html?highlight=button#button
Note that the string and name attributes are mandatory.
Also the name must be the name of the method to call, defined in step 2.
You can find some examples in:
http://hg.tryton.org/modules/account_invoice/file/84a41902ff5d/view/invoice_form.xml#l51

Tracking down error on form or in session

Apologies if this is classic ASP 101, but its been so long since I did any ASP Im struggling to understand / track this error down. What makes it worse is Ive inherited this application and I cant ask the original author..
I have a shopping cart that includes an input checkbox and numerous other fields. When the form is processed and submitted it is run through some javascript and then if all is ok, redirected to another page. (Nothing unusual there). Firebug shows that at this point the value of the check box is different depending on its checked state.
When the form is submitted it goes to another page that iterates over the session.Contents() collection, and builds up a string that is sent to a 3rd party. Using fiddler, it appears that whilst the name of the checkBox is in this string, the value is always 'on'
From reading Google, I see that the session.Contents collection is all parameters that have been placed in the session / application. but a grep across all the files in the project directory doesnt turn up anywhere that the checkbox is added to the session.
So, is the cb there simply because it is on the form or used in javascript, or are there other ways of adding the variable into the session. (Grep on the name doesnt turn up any other instances).
And secondly, if the variable is in the session, no matter how it got there, why is it always set to "on". Im assuming that somehow it has been added to the session and set to On before the form is processed. But the checkbox defaults to unchecked, so Im confused!
Can anyone help explain this, or even suggest how I can track it down / fix it. (The obvious answer is to try to force it into the session with the correct value, but I'd like to know why it is misbehaving rather than just ignore it in case I meet something like this again!
Thanks
I am going to assume that you have already determined that the JavaScript is not modifying checkbox state priort to allowing the submission.
When your form is submitted, the fields that are submitted are in the Request.Form collection. When a checkbox is not checked, it is not part of the Request.Form collection. Therefore, there will be as many checkbox fields in your Request.Form collection as you had checked when submitting, and they will all have the value of their respective "value" property.
If you then add these to the Session.Contents collection, they persist until the session ends. If you never explicitly clear the Session.Contents collection, but submit the form more than once with different values, then the Session.Contents collection will continue to accrue more and more (checkbox_name, checkbox_value) pairs until such time as it contains a (checkbox_name, checkbox_value) pair for every checkbox on your form.
You may wish to write a function that clears each one of your form fields from the session, and call this either after processing a form, or before processing a form (whichever makes sense for your application). Alternatively, just use the Request.Form collection.

Resources