Drupal save data hook_form_alter - drupal

I added a field to a node using hook_form_alter and i can see it fine but now i would like of course that the data enterd in that field is also saved. What do i have to do for this to happen?

When you alter the form, you need to also add a form submission handler to the module; in that way, your module would be invoked when the form is being submitted, and you can save the value of the field you added.
If there are more than one button, then it's preferable to add the submission handler to the specific button you need to act upon. Suppose, for example, that the form has two submission buttons: "Save" and "Delete"; if you add the submission handler with $form[#submit][] = "my module_form_submit"; then the submission handler is called even when the "Delete" button is clicked.
As reported in Form buttons can define custom #submit and #validate handlers:
All forms can have "#validate" and "#submit" properties containing lists of validation and submission handlers to be executed when a user submits data. Previously, if a form featured multiple submission buttons to initiate different actions (updating a record versus deleting, for example), it was necessary to check the incoming $form_values['op'] for the name of the clicked button, then execute different code based on its value.
Now, it is possible to define #validate and #submit properties on each individual form button if desired.
When a specific button is used to submit a form, its validation and submission handlers will be used rather than the default form-level ones. If none are specified at the button level, the form-level handlers will be used instead.
Additionally, The 'op' element in the form values is deprecated and should not be relied upon reports:
As discussed above, each button can have "#validate" and "#submit" functions associated with it. Thus, there should be one button that submits the form and which invokes the normal $form_id_validate and $form_id_submit handlers. Any additional buttons which need to invoke different validate or submit functionality should have button-specific functions. Note also that the 'op' element in the form values, corresponding to the button clicked when there are several in a form in Drupal 5.x, should no longer be relied upon and may not be present.

Related

How to use the same gravity form ID multiple times on the same page

The page setup that I have at the moment is that I have a gravity form that caters for event registration. In this form I have a hidden field that tells me which event the use has registered for.
I display multiple event registrations on one page, so I use the same form multiple times on the same page, but each one with a different "event_name" value.
The problem I'm having is that because each form has the same id, once they submit a registration for one event, all the forms show the "thank you" message and don't allow a resubmit.
Ideally I would like them to not be able to resubmit, but still be able to submit the forms for the other events.
The events are dynamically created, so I can't have a specific form for each event.
Any ideas would be most welcome!
I have done something similar and simply used the conditional part of Gravity Forms. This way, you don't display the form multiple times, but you only show certain fields after they tell you what event they are registering for.

partial Validation for multiple submit button in jsf 1.2

i have one form with dozens of components. currently i am not able to use ajax as its showing some error. i want to validation field only for one submit button but as i have more than one submit button it gives error for others as well. whhen i use immediate for other button action of that button not able to access submitted values.
question is there any way to placed validation for particular submit button when having more than one submit button in a form?
Pls can any one help me in this ?
Use required=”#{!empty param[‘frmName:btnSave’]}” this for the fields with btnSave as id of button on which validation is required.

Cancel button that does not send form data

Inside an UpdatePanel I have two PlaceHolders which are never visible at the same time. In one PlaceHolder I let the user choose an article to edit and when an article is selected it is displayed in the other PlaceHolder. When this happens is set the visibility of the first one to false and turn the other one on.
Now, when the user edits the article there are some fields and two buttons: one button is labeled "OK" and is used to send the form data to the server and the other button is labeled "Cancel" and is used to. It is only when the user clicks the OK-button that I need to send the form data to the server since this is the only situation where bother to handle it. Therefore, can I let the system know that the it should not send form data when the Cancel-button is clicked?
The issue was brought up because at first, I got a RequestValidation-error from the tinyMCE-component that is used for the body of the article (since it contains HTML), when I clicked the Cancel-button. Now I have turned off RequestValidation for this page and I do the validation manually.
But anyway, can I turn off form-data-sending when the Cancel-button is clicked?
Maybe if you prevent the postback when clicking the Cancel button you'll disable the form-data sending. This could be achieved by subscribing to the OnClientClick event of the button and type return false as a value.

Asp.NET AJAX control update field on form

I have a form with several text fields, a couple of drop down lists, and a custom asp.net control.
The requirement I have is that when the values of certain fields ( some are in the main form, some are inside the control ) the user will be alerted that the settings will not take place unless they also restart the processing and will have the option to reset it. If they exercise this option by pressing on the restart button, we execute an additional restart() method call on the server side on top of everything else.
In order to create this alert system I need to know when these required fields have changed. For the fields in the main form/page I store the original values in hidden fields when the page gets created (possible because the page is static). Then when the submit button is pressed I check the current values against the original values stored. If any of them differ I will alert the user.
For the fields in the custom control, I created a boolean property that indicates whether changes in the fields of interest took place. This control is highly dynamic contains a variable number of lists, some of them with over 100 items that can be selected or deselected. So this boolean property is able to identify if the changes I am looking for took place.
The problem is I need to get the value of this property when the user clicks on Submit, otherwise it will not contain the right value. But at the same time I want to avoid a postback. I do not want the whole page to reload. I want to get that value asynchronously somehow.
Since the whole page is stateless the question is how do I accomplish this? How do I accomplish the call to the property? And after I make the call and I get the result where do I store it so it's accessible from javascript code on the client side.
You can try putting a timer control in your ajax panel. Then on a set interval, you will evaluate the boolean property in your code behind.
Alternatively you can check the hidden field "changed" event using JavaScript or jQuery
http://api.jquery.com/change/

How do I trigger a PostBack with arguments from Javascript?

This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.

Resources