Questions tagged [forms]

Use it for questions about the Form API. Don't use it for entity or entity field forms.

Filter by
Sorted by
Tagged with
12
votes
2answers
10k views

load a node content into a lightbox2 (without headers and footer and other blocks)

is it possible to load a node (from an id) into a lightbox 2 without having to load all the additional blocks ? just what the node has to display I want to make some popups on some links to allow ...
12
votes
2answers
7k views

How to rebuild form after AJAX call

I'm attempting to allow the user to dynamically choose a number of fields based on a dropdown box using an ajax call, but I can't seem to get the ajax call to rebuild the form afterwards. <?php ...
12
votes
2answers
7k views

Getting “The form has become outdated” error

I have a multistep node form. Half way through the form through a popup dialog user is asked to login if they have not logged in prior. Upon successful login when proceeding with next page of the ...
11
votes
7answers
13k views

Generating <button type=“submit”> with the form API

I have a heavily themed form to integrate, whose structure is shown below. I am just about there for the most part except for the submit. <form action="#"> <fieldset> <...
11
votes
4answers
10k views

Adding autocomplete for text-field

I tried to implement an autocomplete in text-field for drupal 8 in my custom module all I wanted was to fetch and display the likely title I typed via autocomplete so declared a public function ...
11
votes
4answers
10k views

Print registration and login forms on same page

How do I print the register and login forms on the same page? I think I am really looking just for the form IDs, but if you've got the right render function handy it would be very appreciated.
11
votes
2answers
7k views

How to make custom fieldset collapsed by default in Drupal 8

In Drupal 7 there was option to have fieldset collapsible or not, and to restrict default state of this form element (collapsed, or uncollapsed). Example: $form['contact_data'] = array( '#...
11
votes
5answers
18k views

Numeric field type in Form API

I am trying to add a "number" field type to a form using FAPI: $form['phone_number']['areacode'] = array( '#type' => 'textfield', '#title' => '---', '#width' => '30%', '#align' => ...
11
votes
3answers
6k views

Getting the node object in hook_form_FORM_ID_alter

Follow up to this question but I don't have enough reputation to ask clarification in a comment. I am porting a contributed module to Drupal 8. The sugested code on the above mentioned CR and post ...
11
votes
2answers
14k views

Webform - Submit values from Drupal 7 from to external url

I am a self-proclaimed newbie to creating forms in Drupal. I have a form hosted on a Drupal 7 website (using the webform module) and need to submit the form values to an external url. I've been ...
11
votes
3answers
10k views

How to actually set a maxlength for textarea field in contact form

I need to limit the textarea message of the core module contact form to a certain number of characters. Here are the facts The Form API does not allow one to set a maxlength on textarea field types [...
11
votes
2answers
9k views

How to create a GET-form

For example a filter form on admin areas can best be GET forms, that way they are bookmarkable and can filters be maintained across pager-pages. How can one best use form_api for such forms? Will ...
11
votes
6answers
56k views

How to validate and submit a form using AJAX?

I've created a webform using the Form API. I'm using the #AJAX field option to add AJAX validation to each field. Is it possible to validate and submit the form using AJAX without reloading the page. ...
11
votes
8answers
16k views

Change class on the div wrapper of a form element

Is there a way to add or change a class to a form wrapper (the div's) using the form api? Or is that just done with the theme function ? If so which theme function is used to change that?
11
votes
7answers
18k views

Alter an existing form

We just starting out with Drupal 8 and very quickly we ran into our first problem. How should I alter an existing form in Drupal 8? We need to alter the save method for the node form to do a ...
10
votes
4answers
13k views

How to change node save button text?

I found a discussion with some methods for d6 here http://drupal.org/node/344802 on how to change the node "Save" button text to sg else. Any tips for d7? I would prefer to use form api.
10
votes
3answers
6k views

hook_form_alter execution order

Is there a way to change the order of execution of hook_form_alter in Drupal 7 without changing a module's weight or hacking Drupal Core? I am trying to alter the element added in ...
10
votes
8answers
21k views

How to add a JS confirmation popup when clicking an #ajax button

I have a basic FAPI button input that is #ajax enabled and that's working fine, but I want to add a JS "Are you sure?" confirmation popup when clicking the button before the code actually runs, and I'...
10
votes
5answers
4k views

How do I remove the revision information from a node edit form?

I'm hoping someone can help a Drupal beginner removing a view from my page. The purpose of it is a for data collection. This is the section I am trying to remove:
10
votes
3answers
2k views

Embed one entity form into another and save both

I have a custom entity, that is depending on the user entity. In fact, it is even so depending that I felt it made sense to display my entity form within the user profile form: The problem I am now ...
10
votes
3answers
35k views

Programmatically set the default value for a field

In Drupal 6, we could use PHP to set default values, but the same code cannot be used with Drupal 7. I am trying to do this through hook_form_alter(), but the code doesn't work. function ...
10
votes
3answers
6k views

Multistep/wizard forms

I'm trying to create a multistep/wizard form for Drupal 8. User fills in a first name, last name fields Clicks on the next button Fills out more information Clicks on the submit button There are ...
10
votes
1answer
7k views

Why is my form themed as table not posting data correctly, or displaying correctly?

I have created a form in the admin. $form['things'] = array( '#prefix' => '<div id="things">', '#suffix' => '</div>', '#tree' => TRUE, '#theme' => 'table', '#header' ...
10
votes
1answer
501 views

Difference between $form_id and $form['#id']?

What's the difference between $form_id and $form['#id'] in hook_form_alter() ? Why they have not the same values?
10
votes
2answers
7k views

How to hide and/or prevent access to fields in a form, such as group_audience for organic groups

This has been troubleing me for a while.. When i try to hide fields like the group audience field, or the publishing setting from a form, on validation the form set both of these field to a null value,...
10
votes
1answer
4k views

What is the difference between drupal_get_form and drupal_retrieve_form?

What is the difference between drupal_get_form() and drupal_retrieve_form()? Can someone explain the difference? I look at the API, but the description seems similar to me.
10
votes
3answers
3k views

AJAX calls in CTools Content Type Plugin?

I'm building a CTools Panels Content Type (i.e., that thing you insert into panels when adding content, not a type of node) and I am trying to use a form item's #ajax attribute to set some default ...
10
votes
2answers
9k views

Customize days that can be clicked on a Calendar popup

I am creating a form in Drupal 7 with the form API. Right now, I can create an ordinary calendar input field with the following code: $form['services_tab']['schedule_datepicker'] = array( '#...
9
votes
5answers
8k views

How do you alter a form error?

How do you alter or remove a form error produced by Drupal or another module on node insert/save, for a particular content type. I'm looking for a method which I can use in a module. I've tried ...
9
votes
2answers
7k views

How to programmatically update the allowed values of a list field?

I have created a select list for a Profile2 account in Drupal 7. In the allowed values list, I have added some values. Now what I would like to do is programmatically update the values in that list (...
9
votes
3answers
13k views

Empty value “- Select -” does not appear for select fields with states requirement

When I create a standard select box using the Drupal 7 form API, the first value (also the one selected on a fresh 'blank form') is <option selected="selected" value="">- Select -</option>....
9
votes
5answers
17k views

How do I add a CSS class to a form label?

I want to add a CSS class to a label on a page that is using Drupal 8 Form API. I have been unable to find a reference online on how to apply that. I have used the following workaround, but it ...
9
votes
5answers
18k views

How to avoid “An illegal choice has been detected” when updating select list values via ajax?

I have four entity reference fields to user entity in a form. Host form is node edit form. Three of them are select lists, last is Inline Entity Form. After adding a user entity via IEF I'm trying ...
9
votes
5answers
16k views

Preview image after uploading using Form API

I uploaded an image file using the managed_file Form API type, but after uploading the image it does not appear as a thumbnail beside the field. What is rendered is the file name of the image with a ...
9
votes
1answer
3k views

What is the difference between hide() function and unset()?

What is the difference between hide() and unset()? I want to hide some elements, I found hide() and unset(), but I don't know which one I should use.
9
votes
1answer
14k views

Add text after input field with form api (field-suffix)

I'm creating a form with a textfield. After the input field I'd like to have some text. Like this: Label * |________| some text Field description The code I use is: $form['a_textfield'] = array( ...
9
votes
6answers
10k views

Disable an option for the select form field

How do I disable an option in the select form field? $form['feed'] = array( '#type' => 'select', '#title' => t('Display of XML feed items'), '#options' => array( 'title' => t('...
9
votes
6answers
18k views

How can I add a class to a label?

I need to add a class name to certain labels created by Drupal's form API like this: $form['name'] => array( '#type' => 'textfield', '#title' => 'Prénom' ); If I use the following, the &...
9
votes
3answers
9k views

Disable all form elements when doing AJAX

I have a form where every user input field is AJAX enabled, and I'd like to disable all of the form elements when one of them is changed - is it possible?
9
votes
2answers
4k views

How do you change the location of `form_set_error` output?

Is there a way in Drupal 7 to change the location of the output of form_set_error? At the moment, it calls drupal_set_message which queues all the form's errors to the top of the screen. What I ...
9
votes
3answers
20k views

Form Api File upload

I have the following code in my form. $form['new']['upload'] = array( '#type' => 'file', '#title' => t(''), '#size' => 40, ); On the submission handler, it returns the file name but ...
9
votes
4answers
19k views

How to make form buttons call only javascript?

I'm experimenting with JavaScript and Drupal forms. Currently I'm trying to make a button in the administration form of a module that will use JavaScript to append options to a select list. The ...
9
votes
3answers
9k views

Can I use ajax to target multiple form elements from one input?

I'm trying to use ajax to update two different parts of a drupal form from just one form input blur. I have the standard ajax stuff on my input: $my_form['my_input']['#ajax'] = array( 'callback' =&...
9
votes
1answer
14k views

Changing the registration form

I would like to revise the default new user registration form in Drupal 7. Rather than to requst "username", I want to request "full name" with helper instructions to enter the text "username is your ...
9
votes
6answers
15k views

E-mail validation form api

What is the best way to handle server side e-mail validation in the form API? Can it be implemented somewhere inside part of the form described below or are there other options? $form['address']['...
9
votes
4answers
14k views

How do I print results after submitting a Drupal form?

I have a simple Drupal Module setup which has a single textbox. I can return what was typed in the textbox using drupal_set_message(): function firstmodule_form1_submit($form, &$form_state) { ...
9
votes
1answer
6k views

How to use #element_validate and #value_callback in Drupal 8

I have followed the official Form API Documentation, but this code don't work for me on Drupal 8: public function buildForm(array $form, FormStateInterface $form_state) { $form['upload'] = ...
9
votes
5answers
8k views

Prevent Multiple Form Submissions (Server side)

I'm running into a problem where a user can submit any form built by the Form API multiple times (fast clicking resulting in multiple requests). I've put in the basic client side (javascript) ...
9
votes
2answers
2k views

Need to add HTML characters to Select list

I have a form using the FAPI that has a select list of currency values as one of its inputs. I need to be able to add HTML characters such as &euro; $pound; &yen; The problem is that Drupal ...
9
votes
4answers
13k views

How to add taxonomy term reference field in a custom drupal form

A menu item is defined with drupal_get_form as callback function and returning the form from call back function. How can I add taxonomy_term_reference field to this form? $items['files/add'] = array( ...