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.

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 ...
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'] <- ...
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 ...
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 $...
33
votes
1answer
19k 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.
31
votes
13answers
21k 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 ...
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 ...
26
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 ...
26
votes
2answers
23k 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.
25
votes
2answers
50k 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 ...
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 ...
23
votes
5answers
7k 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 ...
22
votes
10answers
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, ...
22
votes
6answers
15k 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) ?
21
votes
8answers
56k 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, &...
21
votes
5answers
36k views

Adding form placeholders to text input fields

Is there a way to convert a regular form and add placeholders with a module, or should this be done with a form template or with jQuery?
20
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?
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!...
20
votes
3answers
7k 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
4answers
8k 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 ...
19
votes
10answers
31k 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 ...
19
votes
5answers
18k 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\...
19
votes
3answers
15k 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.
19
votes
3answers
28k 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]; $...
19
votes
1answer
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
3answers
13k 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 ...
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 ...
18
votes
4answers
27k 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 ...
16
votes
3answers
10k 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 ...
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 ...
16
votes
2answers
10k 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 ...
16
votes
12answers
40k 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; $...
15
votes
4answers
15k 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 ...
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 ...
15
votes
2answers
21k 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 ...
14
votes
2answers
14k 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' ...
14
votes
3answers
27k 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(). ...
14
votes
2answers
18k views

Redirect after form submission

I create custom Form in Drupal 8 ,I remember we can redirect to custom path in Drupal 7 with set $form_state['redirect'] = 'mycustompath'; in custom submit handler, but how can redirect User after ...
14
votes
3answers
25k views

How to check a checkbox by default in forms api

I wonder how I can check a checkbox by default? Is #default_value the wrong attribute? $form['ios'] = array( '#title' => t(''), '#type' => 'checkboxes', '#...
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) {...
13
votes
1answer
12k 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' =&...
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 ...
13
votes
3answers
18k views

How to disable the submit button after clicking?

Drupal doesn't seem to prevent multiple form submits, even on node pages. This QA suggests fixing it on the server side. How could I disable the submit button after clicking? Any other solutions?
13
votes
6answers
8k views

How to make a field readonly using hook_form_alter

I am trying to make some of the Drupal profile form fields as read-only in edit mode. I am using the following code in hook_alter $form[field_organisation_company_name][und][0]['#after_build'][]='...
13
votes
3answers
14k views

How do I add client-side form validation to a Drupal form?

How do I add client-side form validation to a Drupal form? For example, in the comment module. If the vistor don't enter the email address, I want to popup a window that says: "please enter the ...
12
votes
7answers
15k 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?
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 ...
12
votes
6answers
9k views

Attach WYSIWYG to Drupal Form

With a custom Drupal form, how can you attach WYSIWYG functionality to a textarea with the WYSIWYG module?
12
votes
3answers
12k views

How to keep cache_form small

I have noticed in mysql that cache_form.MYD table is 14GB. Why did it grow so big? Is it safe to empty it? and how can I avoid this happening in the future? Thanks
12
votes
2answers
6k 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 ...