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
51 votes
7 answers
64k 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 ...
  • 6,453
41 votes
3 answers
61k 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 $...
  • 1,983
41 votes
1 answer
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'] <- ...
  • 2,440
41 votes
1 answer
19k 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 ...
  • 6,903
37 votes
1 answer
23k views

What is $form_state used for?

What is $form_state typically used for in the context of the Form API if it is used as an argument? Specifically, I'm looking for an example of when it is used.
  • 6,492
35 votes
2 answers
63k 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 ...
  • 6,027
32 votes
13 answers
25k views

How can I disable a single checkbox in a 'checkboxes' FAPI element?

Title basically says it all, I want to disable a single checkbox of a checkboxes type FAPI element. I don't want to do if with JavaScript, nor is changing it from checkboxes to multiple checkbox ...
  • 3,692
32 votes
8 answers
24k views

How can I make a form element required with states?

I have a drop down list that displays various fields based on what is chosen and I know that I can toogle the visibility with states but when I try to use required the * span is display but it isn't ...
  • 669
31 votes
8 answers
73k views

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

How do I add a custom validation handler to an existing form (or form field) in Drupal 8? I have a form that I haven't created. I want to add my own validation rules on some fields when the form is ...
30 votes
5 answers
58k 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 ...
  • 1,000
29 votes
3 answers
73k 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(). ...
  • 21.4k
29 votes
2 answers
27k views

How do I set allowed extensions for a managed_file form field?

Not having any luck find this in Drupal documentation or developer examples. I need to set a managed_file form field to only allow zip files in a Drupal 7 custom module.
  • 393
28 votes
4 answers
22k 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 ...
  • 21.4k
27 votes
7 answers
54k views

Adding form placeholders to text form elements

Is there a way to add placeholders to a form with a module, or should this be done with a form template?
  • 6,492
27 votes
7 answers
47k 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?
27 votes
3 answers
31k views

How do I pass parameters to the form builder?

I have the following routes in module_name.routing.yml. module_name.usergroup_delete: path: 'module_name/usergroup/delete/{arg1}' defaults: _form: '\Drupal\module_name\Form\DeleteUserGroup' ...
  • 2,260
26 votes
9 answers
80k 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, &...
  • 2,039
25 votes
5 answers
10k views

Difference between $form_state['values'] and $form_state['input']

Can someone please tell me the difference between these two variables? $form_state['values'] $form_state['input'] Optionally some example of action they are used with. I'm wondering especially about ...
  • 395
24 votes
6 answers
37k views

How do I render a form?

What is the alternative to drupal_get_form('user_login'). in Drupal 8? I am using the following code in theme_name.theme, but it doesn't work. // Include namespace for userLoginForm. use \Drupal\...
23 votes
3 answers
14k views

Multiple Values to Trigger #states

How can I have multiple values trigger the Form API's #states? Say for example, I wanted this field to be visible not only if the value is 5 (currently works below), but wanted to make the field ...
  • 7,884
23 votes
10 answers
12k views

Adding HTML5 placeholder to all drupal forms

I'm trying to add the placeholder attribute to all textfields across my website, but I'm having no luck. I've created a module that overrides hook_form_alter function mymodule_form_alter(&$form, ...
21 votes
6 answers
16k views

How to upload multiple files at once?

We need to upload multiple files of different types, is there a module that will check for valid mime types and upload only the valid files, (based on site settings, eg: png, doc, jpg, txt) ?
  • 1,367
21 votes
5 answers
29k 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 ...
  • 3,034
20 votes
7 answers
48k views

Redirect after form submission

I created a custom form in Drupal 8. I remember we can redirect to custom path in Drupal 7 with $form_state['redirect'] = 'mycustompath'; in a submission handler. How can I achieve the same in Drupal ...
  • 21.4k
20 votes
3 answers
17k 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!...
  • 1,357
20 votes
3 answers
31k views

Passing arguments to drupal_get_form()

How can I pass arguments to drupal_get_form() in Drupal 7? function new_menu_callback() { $vars = some_example_function(); // returns: array(1 => 'one', 2 => 'two'); $output = $vars[2]; $...
  • 7,884
20 votes
3 answers
8k views

Remove subject field from comments

Is it possible to remove the subject field on comment forms? I find it to be kind of useless and redundant, since the subject of the comment is usually the title of the article being read, at least ...
20 votes
3 answers
19k 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 ...
  • 5,463
19 votes
3 answers
16k views

Embed a "node add" form in a page

I'm looking for a way to embed a "node add" form on a landing page. I was using Form Block, but it appears to bring many error because I was using a development snapshot version.
  • 325
19 votes
1 answer
8k views

Saving a field of type 'text_format' as a system setting variable

I'm attempting to give my content editors the ability to change a variable for a custom module. This variable can have html. I don't want to require them to have any knowledge of HTML, so I want to ...
19 votes
4 answers
9k views

Easiest method for conditional fields in a form

What is the easiest way to get some javascript magic for enabling/disabling form fields based on another field's value? This sounds like something there should be an helper for somewhere, but I can't ...
  • 578
18 votes
2 answers
20k views

How can I upload files with the form element type file?

I want to upload a file and I have to use form element type as 'file'. In submit function I am not able to get any proper data related to the file which I am uploading. SO Can anyone please help me to ...
  • 874
18 votes
6 answers
31k 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 ...
  • 181
18 votes
3 answers
35k views

How do I show a checkboxes form element that is selected by default?

How do I show a checkboxes form element that is selected by default? I tried setting #default_value as in the following code, but it doesn't work. $form['ios'] = array( '#type' => 'checkboxes', ...
  • 702
18 votes
3 answers
16k views

Add classes to form select option item

How can I add classes to a form option tag without JS? At the moment in Form API I can pass a keyed array like this array( '0' => 'option 0', '1' => 'option 1', ) and I'll get html like ...
17 votes
5 answers
26k 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 ...
  • 181
17 votes
3 answers
24k views

How do I alter the form submission handler?

For some reason I need to replace some characters (if exist) in every single textfields in any form just right before they stored in database. In other words I need a hook in which I could get all ...
17 votes
4 answers
28k views

Adding extra fields to Site Contact Form

This seems strangely difficult to me. What I thought would just be a case of adding extra fields a bit like adding fields to Content Types. But no. I can't locate any facility to add say a phone ...
17 votes
12 answers
54k views

Set select list default value [closed]

I have select list with options: $options = array(0 => '0', 500 => '500', 1000 => '1000'); I'm trying to set it's default value to 500: $form['price']['min']['#options'] = $options; $...
  • 401
16 votes
3 answers
14k 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( '#...
16 votes
2 answers
11k views

Change $form_state in AJAX callback

I am creating a form which uses the form API '#ajax' attribute, and I need to make changes to $form_state when the AJAX-enabled button is pressed. Here's what I've got: Inside my form builder ...
  • 311
15 votes
3 answers
24k views

How can I implement AJAX form submission?

My task is to submit a contact form via AJAX and then to show a "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form. I found ...
15 votes
3 answers
17k 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 ...
  • 487
15 votes
1 answer
13k views

How do I use a submit callback on a Drupal admin settings form?

I'm using this code to make my admin form: function custom_orders_admin(){ $form = array(); $form['custom_ignore_terms']['ignore_terms'] = array( '#type' => 'checkboxes', '#title' =&...
15 votes
2 answers
21k 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 ...
15 votes
3 answers
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) {...
  • 2,765
14 votes
7 answers
20k views

How can I know which button was clicked?

I have a form with a "Submit" and a "Cancel" buttons. In hook_submit(), how do I know which button was clicked?
  • 3,889
14 votes
2 answers
16k 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 ...
  • 1,888
14 votes
4 answers
17k views

Redirect to a custom page after a node has been added or edited

Using hook_form_alter, how can I redirect a user to a a custom page after creating or editing a node instead of the node that was created or edited? The current code for hook_form_alter() is the ...
  • 7,884
14 votes
6 answers
30k 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' => ...

1
2 3 4 5
136