Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [forms]

Don't use it for entity or entity field forms.

41
votes
1answer
6k views

Why is hook_form_alter() so messy?

Here's an example I just ran into. I'm trying to put some HTML in the label of my field. $form['field_feedback']['und']['#title'] <- has the title $form['field_feedback']['und'][0]['#title'] <- ...
27
votes
5answers
54k views

Dynamic select list in the form (dependent dropdown)

I'm using Drupal seven. I want to make the options in a select list be dependent on the value chosen in another select list in a form. I'm sure this has been asked many times before, but I am having ...
13
votes
3answers
16k views

How can I implement AJAX form submission?

My task is to submit contact form via AJAX and then to show "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form. I found some examples ...
25
votes
2answers
51k views

Custom validation for a form?

I need to add custom validation to a form, lets says add article form. I was expecting something like hook_FORM_ID_validate() to handle this, but cannot find anything relavant in the API ...
49
votes
7answers
56k views

How to a use a template file to theme a form?

While nodes, comments, blocks and many other things in Drupal are themed using theme template files (like node.tpl.php), forms are a different story. There are no theme template files for forms. How ...
15
votes
3answers
29k views

Add a custom submission handler to a form

How can I add a custom form submission handler? I tried adding $form['#submit'][] = 'mymodule_form_submit'; or $form['actions']['submit']['#submit'][] = 'mymodule_form_submit'; to hook_form_alter(). ...
7
votes
3answers
12k views

Resolve the error “An illegal choice has been detected…”

I have written code like below and all works fine, but while submitting the form I get the error below. I am trying to filter the second dropdown based on the first drop down selection. An illegal ...
23
votes
5answers
26k views

Show forms in a modal window [closed]

I have a form and I am using Drupal 7. I need to show this form in a popup when a user clicks a link. The user should then be able to complete the form whilst in the pop up window. I can use anything ...
36
votes
3answers
54k views

Adding CSS and JS to form with attachments

I need to add some external and local CSS and JavaScript files to a form, but I can't seem to find the right way to do this. Do I simply add the paths and URLs for the JS and CSS files? I assume $...
21
votes
8answers
57k views

How can I find the form ID of a form?

How do I get the form id of my form? I've just begun to make my modules and I'm at a standstill. I got this code from someone else and am trying to customize it: function hook_form_alter(&$form, &...
5
votes
2answers
3k views

Change a normal submit 'input' type to 'button' type with button tag

Is it possible in Drupal 8 to change a regular form submit button from Drupal into a button tag? This is the output I need to achieve: <button type="button" class="search-box__button"> <...
15
votes
2answers
19k views

Form within a block

I'm using drupal_get_form('event_signup_form') to embed my custom form inside a block. The form is being rendered as intended, but submitting the form triggers nothing. The form function itself is ...
3
votes
2answers
7k views

Date popup form field doesn't show the default value correctly

I am creating a custom form with "date_popup" form fields. The only issue I have is that the default_value for the date popup form field shows only the first digit of the year, not the whole date. ...
8
votes
2answers
17k views

How can I display a change password form on its own?

I have a need to place a user's password reset form in areas outside the context of the user edit page. This Change Password module looked promising; however, it is only available for drupal 6 and ...
39
votes
1answer
18k views

Managed file: the file used in the field may not be referenced

I am having some issues with the managed_file type. Whenever I try to edit and save a form that has a managed_file element in it an error occurs: The file used in the Picture field may not be ...
21
votes
6answers
26k views

How to redirect to a page after submitting a form

I have a custom module that displays a form. Upon submitting the form, I want to redirect the user to a different page. How can I do that?
7
votes
1answer
3k views

How to implement a multi step approval with webforms/entityforms based on form components?

Is it possible to invoke multi-step submission workflow using either Webform or Entityform? Here is the scenario of workflows: An employee submits the form Superviser gets notification,...
6
votes
7answers
7k views

How to replace the search submit button with a Font Awesome character?

I would like to replace the search submit button with a Font Awesome character like the Magnifier (Search Icon) character, and put Search the site inside the search box. How can I achieve something ...
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' =&...
2
votes
2answers
2k views

How to force a redirect (override the destination query string in the URL)

I have a custom form submit callback to redirect users to a specific page: function MYMODULE_authfrontpageredirect_callback(array &$form, FormStateInterface &$form_state) { $url = \Drupal::...
20
votes
3answers
15k views

specify $form elements to be excluded from display?

I want to hide certain elements in form using form_alter or a template file. I have read somewhere that in Drupal 7 , we can specify $form elements so it will not display in form, but I can't remember!...
18
votes
3answers
18k views

Form Redirect not working if 'destination' is in URL

In one of my forms, I'm trying to set a $form_state['redirect'] so the form will go to that destination after the user clicks one of the action buttons. If I inspect $form_state['redirect'] both ...
3
votes
1answer
12k views

How to theme webform elements (radio buttons etc.)?

I would like to know how to theme Drupal 7 Webform elements? I want to change the HTML structure of every element, not just wrap form with div or something like that. And I want to leave theme's ...
14
votes
3answers
16k views

How do I add an additional submit handler in commerce checkout form?

How do I add an additional submit handler? I have tried this but it did not work. function c_coupon_form_alter(&$form, $form_state, $form_id) { drupal_set_message($form_id); switch($form_id) {...
6
votes
2answers
2k views

How to customize the default search block form template?

How can I customize the default search block form template, I now use below template to display a search form block, but this code doesn't allow me to edit and customize each part of this form ...
16
votes
4answers
19k views

How do you implement a custom submit handler in hook_form_alter()?

I've been updating one of my Drupal 7 modules to 8 and I'm having an issue with my form alter. I'm adding a validation function and a submit handler. The validation function is getting called just ...
5
votes
6answers
7k views

How to place Contact form in block?

I have to embed a contact form inside of a block, and I'm trying to use the entity.form_builder service, But I'm not sure which entity I should give it, I found Message and ContactForm entity, so what ...
2
votes
3answers
6k views

How to pre-populate multiple “field collection” fields in node form [closed]

I'm building a multistep node add form where you upload a zip of files on page 1, let the module read file names into an array when the uploader presses the "next" button, and then allows the uploader ...
2
votes
1answer
927 views

Custom registration twig template

What would be the best way to make a custom registration twig template ? I have implemented the hook : function THEMENAME_theme_suggestions_form_alter(array &$suggestions, array $variables) { ...
1
vote
2answers
977 views

How to render specific form elements from the *.theme file

I am using Drupal 8 and would like to customize how form elements are being displayed. Specifically, I don't like how uneditable, populated textfields are displayed as plain text. I would have it ...
19
votes
10answers
32k views

How do I add a custom validation to an existing form/field?

It's all in the title: how to add a custom validation to an existing form (or form field) in Drupal 8? I have a form that I haven't created that I want to alter to execute my own validation rules on ...
24
votes
4answers
16k views

Difference between create new and edit node in form alter

I have an alter section for a particular content type. In the form alter how can I find out whether it is create new form or edit form? With dsm($form) in from alter I can get results with several ...
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 ...
9
votes
4answers
18k 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
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) { ...
5
votes
2answers
7k views

Theme Drupal Form API checkboxes as a grid

I have a custom form that displays a form element of about 2 dozen checkboxes. I would like to output them 3 per row, in a table if possible. How can I go about doing that? $form['preference'] = ...
12
votes
2answers
12k views

“Illegal Choice has been detected” [duplicate]

I am dynamically populating <select> throught javascript in Drupal 7. The system gives me an error on submit: "Illegal Choice has been detected" How to mark, that this <select> must skip ...
3
votes
5answers
2k views

How can I allow anonymous visitors to submit content?

I am working on a website that displays a list of historical events. Most of the events have been added by a small group of users that manage the site. However, I would also like to add the ability ...
3
votes
2answers
5k views

How to trigger Ajax forms machinery

I'm working on ajaxifying a form which uses Hierarchical Select widget. I need to update a text field based on the value of HS and a couple of other fields. I want to use Drupal forms Ajax API. I ...
2
votes
1answer
5k views

AJAX callback on form item generated by AJAX

I'm trying to add AJAX callback to form element that was previously rendered by AJAX but with no luck. Code below. A bit of description. There are three main methods here: buildForm - base form ...
5
votes
2answers
8k views

How to omit form's validation at AJAX button click?

I have an AJAX button that should update form fields according to data already provided by user: 'ajax_button' => array( '#type' => 'button', '#value' => t("Scan for tags"), '#...
6
votes
1answer
4k views

form_state->setRedirect(to_a_new_node_page) from the submitForm function, route “does not exist” issue

I created a custom module, It generate a custom Form in a page. When I submit my form, I create a new node. So, I am redirect to the form page but I would like be redirect to the new node page. I add ...
5
votes
3answers
5k views

default value for entity reference field

I have a form with an entity reference field and I need to add a default value. I've tried $form['field_op_product']['und'][0]['target_id']['#default_value'] = $product_node->nid; but, that doesn'...
4
votes
1answer
3k views

Is it possible to define hook functions in template.php instead of the module?

Theme functions are defined in the template.php but is it possible to define hook functions in there? for example There is a content type called phone-book, its add content form ID is phone-book-node, ...
4
votes
1answer
2k views

How do I load a form with a specific form display (form mode)?

On a custom page I'm trying to load an user edit form with a specific form display. I'm able to load the form however I can't figure out how to load the form with the specific display. This is what I ...
3
votes
6answers
6k views

How to hide default “N/A” option for checkboxes?

I have a "gender" (checkbox) option that I want to show on the user profile page (but not on the registration page). The options are "Male" (default) and "Female". Since I don't make the field ...
2
votes
9answers
10k views

Render form in template

I have made the following .tpl.php template: <?php print drupal_render($element["term"]); ?> <?php print drupal_render($element["search"]);?> <?php print drupal_render($element["...
4
votes
5answers
3k views

How to programmatically create a title for a new node?

I want the title field of each new node of the Agenda content type to be constructed automatically from a date field in that same node. I have variously tried setting the #default_value and #value on ...
3
votes
2answers
764 views

Cannot deal with multiple iterations of same form on one page

I am designing a site that has a variety of points in which I need to render "workflow" buttons that are used to send actions to an external application. I used the form API to generate these ...
2
votes
2answers
232 views

Custom Login form question

In my website have two types people login one is designer another is reader, i want people come to login my website by role. Mean designer go to design login form to login. Reader go to reader login ...