Questions tagged [forms]
Use it for questions about the Form API. Don't use it for entity or entity field forms.
738
questions
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'] <- ...
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 ...
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 ...
29
votes
3
answers
79k
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().
...
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 ...
15
votes
3
answers
25k
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 ...
9
votes
2
answers
8k
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">
<...
7
votes
3
answers
17k
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 ...
26
votes
9
answers
82k
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, &...
33
votes
8
answers
26k
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 ...
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 ...
4
votes
2
answers
4k
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 ...
3
votes
1
answer
4k
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) {
...
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 $...
31
votes
8
answers
76k
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 ...
27
votes
7
answers
49k
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?
9
votes
3
answers
8k
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 ...
2
votes
1
answer
1k
views
Create any number of paragraph fields in a multiple value field on node create form
I have a paragraphs field that takes a term reference.
I need to count how many terms there are for a specific vocabulary and then create that many instance of the paragraphs multiple value field for ...
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 ...
8
votes
3
answers
10k
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' =&...
3
votes
2
answers
8k
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.
...
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 ...
9
votes
2
answers
18k
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 ...
9
votes
2
answers
5k
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::...
7
votes
1
answer
4k
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,...
7
votes
7
answers
12k
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 ...
7
votes
3
answers
21k
views
How do you theme a content type's create/edit form?
Like it says -- I've got a D7 content type whose editing form needs to be themed. I did this all the time in D6 by registering theme functions in template.php and writing the associated theming ...
5
votes
2
answers
2k
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 ...
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 ...
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!...
17
votes
5
answers
27k
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 ...
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) {...
7
votes
3
answers
6k
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 ...
6
votes
7
answers
11k
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 ...
3
votes
1
answer
13k
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 ...
3
votes
3
answers
7k
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
2
answers
14k
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
...
2
votes
1
answer
2k
views
Adding Constraint to entity referenced Paragraph field
I'm attempting to follow this documentation to add a custom constraint to a field in a Paragraph but I'm having trouble in adding constraint to that field.
Currently I have book content type with ...
15
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 ...
11
votes
2
answers
15k
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
4
answers
15k
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) {
...
10
votes
4
answers
21k
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
2
answers
12k
views
How to implement "Add another item" for custom forms?
I am writing a module which is basically a form that creates a relation between two end points.
I want a user to be able to create multiple relations at once. I want to add a button which says "add ...
9
votes
2
answers
23k
views
How to customize the user login form?
I'm trying to customize user login form in /user/login page and I want to use a hook_form_alter function to add some awesome style and placeholder to each of user login form fields and remove or hide: ...
6
votes
1
answer
8k
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
2
answers
6k
views
Ajax submission leads to => An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (2 MB) that this server supports
So i'm trying to submit a form using ajax, in my form i have the following:
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
...
5
votes
2
answers
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'] = ...
5
votes
5
answers
5k
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 ...
5
votes
1
answer
8k
views
Load form via ajax and submit by ajax not working
I have loaded a form via ajax and submit by ajax not working. But if I load form directly by url ajax submission is working. Form is loaded by ajax is using jQuery.
$('#button').on('click', function (...
5
votes
2
answers
9k
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"),
'#...