I have a node/add form.Using hook_form_alter i have added a textfield and a ajax submit button.I do fecth data with that textfiled and button.
function hook_form_alter(&$form, &$form_state, $form_id) {
$form['textfield'] = array(
'#title' => t('Add Field'),
'#type' => 'textfield',
'#required' => true,
'#weight' => -1,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Field'),
'#weight' => -1,
'#ajax' => array(
'wrapper' => 'node-form',
'callback' => 'submit_callback',
'effect' => 'fade'
),
);
}
and onsubmit of the ajax I am getting the values which I want. But now I want to add few fields of node/add form with the result of that ajax submit.I have got stuck for how to go further with the implementation for this.
Can anyone help me out & let me know how to do this?
I can only think of two options:
1. Store results in $_SESSION.
2. Store results in database (preferably using ctools).
something like this
<?php
$_SESSION['my_data'] = 'somedata';
unset($_SESSION['my_data']);(To unset)
?>
Hope this will help you.
Related
I currently have a form with a submit button that I would like to leave disabled unless any checkbox(es) in a group of them is checked. Currently, no matter what I do the button is not disabled. Here's the code I'm using:
$form['enrollments']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Change'),
'#states' => array(
'disabled' => array(
'input:checked' => array('length' => 0),
),
);
The checkboxes all share a shame name in array form like "students[12345]". So, if any one of those "students" checkboxes is checked the button should be enabled. However, given the code above the button is always enabled.
I had a similar issue just come up with a Drupal 8 form using checkboxes and submit. The same should work for Drupal 7 forms, although the array definitions of [] may need to be updated to array().
My checkboxes, of which I have 8, do not share an explicitly common name, so in order to accomplish this without adding 8 unique lines of :input[name="..."] I simply added a common data attribute to the checkbox items, which I could then target with the #states selector.
I also added a #required => TRUE to defend against the form somehow being submitted w/o any options selected. This causes the page to refresh and display an error highlighting the checkboxes, indicating a selection is required.
$form['node_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Select content types for report.'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => array_keys($options),
'#attributes' => [
'data-type' => ['node-type-checkbox']
],
'#required' => TRUE
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Download'),
'#button_type' => 'primary',
'#states' => [
'disabled' => [
':input[data-type="node-type-checkbox"]' => ['checked' => false],
]
]
];
I think this link can solve your problem:
https://www.lullabot.com/articles/form-api-states
Also, you can use this if you want to show some thing upon visibility of checkbox:
'#states' => array(
'visible' => array(
':input[name="nameofselectororfield"]' => array('checked' => TRUE),
),
),
This worked for me...
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#states' => array(
'disabled' => array(
':input[name="student12345"]' => array('checked' => FALSE),
),
),
);
How I want it to work:
I have list of buttons(type button) that trigger an Ajax call that add the that item to a list. This works as exactly as I want it except for the button looks ugly .
The problem:
When I try to replace the "button" with an "image button" the form is submitted, which is not what I want. Is there a way to add an image to a button that do not trigger a submit? Can I disable the submit for "image button"? Or shall I add the image using css on the button?
What is the difference between "button", "image button" and "submit"?
To make the type image_button act like an button is looked in system.module and found "'#executes_submit_callback' => TRUE", so change this to false will prevent submit function to be called on your image_button.
Code from system.module:
$types['button'] = array(
'#input' => TRUE,
'#name' => 'op',
'#button_type' => 'submit',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#theme_wrappers' => array('button'),
);
$types['image_button'] = array(
'#input' => TRUE,
'#button_type' => 'submit',
'#executes_submit_callback' => TRUE, // <--- This is why submit is triggered
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#return_value' => TRUE,
'#has_garbage_value' => TRUE,
'#src' => NULL,
'#theme_wrappers' => array('image_button'),
);
Another thing that you might encounter is the validation error, to remove it just add the "'#validate' => array()" or if you want to run validation but ignore errors use "#limit_validation_errors' => array()". This also apply for button.
Here is an example where you can experiment the above things and see when the validation and submit callback is triggered. Also a checkbox is included to show when validation errors occurs.
function button_menu()
{
$items = array();
$items['button'] = array(
'title' => 'Button',
'page callback' => 'drupal_get_form',
'page arguments' => array('button_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function button_form($form, &$form_state)
{
// Add to prove the point of how validation is working
$form['checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('checkbox'),
'#default_value' => FALSE,
);
$form['button'] = array(
'#id' => 'button_1',
'#type' => 'button',
'#name' => 'test1',
'#value' => 'test1',
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['image_button'] = array(
'#id' => 'image_button_1',
'#type' => 'image_button',
'#src' => '/themes/bartik/logo.png', // hope you still have bartik theme
'#executes_submit_callback' => FALSE, // This line removes the submit callback
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
// Just some code to show what button was pressed
if (array_key_exists('triggering_element', $form_state) &&
($form_state['triggering_element']['#id'] == 'button_1' || $form_state['triggering_element']['#id'] == 'image_button_1'))
{
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>'. $form_state['triggering_element']['#id'] .'</p></div>',
);
}
else {
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>nothing</p></div>',
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function button_test_callback($form, $form_state)
{
return $form['markup'];
}
function button_form_validate($form, &$form_state)
{
// To display when validation is triggered
drupal_set_message('button_form_validate');
// Only when submit button is pressed we care about this error.
if ( $form_state['values']['checkbox'] == 0) {
form_set_error('checkbox', 'checkbox not checked');
}
}
function button_form_submit($form, &$form_state)
{
// To display when submit is triggered
drupal_set_message('button_form_submit');
}
I am trying to develop a module where in my form needs to be rendered in a tabular form. Everything is working fine except the submit button. It is neither calling default _validation or _submit functions nor _form_alter functions. I don't know where I am missing.
Here is the code for my .module file
<?php
function mytheme_menu(){
$items = array();
$items['enqform']=array(
'title' => 'Enquiry Form',
'page callback' => 'mytheme_function',
'access arguments'=>array('access content'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function mytheme_function(){
return theme('enquiry')
}
function mytheme_theme() {
return array(
'enquiry' => array(
'render element' => 'form',
'template' => 'custompage',
),
);
}
function enquiry_form($form, &$form_state){
$form['efname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE,
);
$form['elname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function enquiry_form_validate($form, &$form_state){
}
function enquiry_form_submit($form, &$form_state) {
db_insert('form_example')->fields(array(
'efname'=>$form_state['values']['efname'],
'elname'=>$form_state['values']['elname'],
'active' => 1,
)
)->execute();
}
and my .tpl.php page looks like this
<?php$form = drupal_get_form('enquiry_form');?>
<h2>Please enter details here</h2>
<?php print render(drupal_render($form['efname']));?>
<?php print render(drupal_render($form['submit']));?>
<?php print render(drupal_render_children($form));?>
When I use 'drupal_get_form' as page callback in my hook menu, submit is working fine, but the purpose of template is of no use. It would be a day saver if someone can guide me where and what am I missing in this code.
I think that you should not render the drupal_render_children, you should just print drupal_render_children by itself.
<?php print drupal_render_children($form); ?>
I am working on Drupal 7. I want to display a form on "Error! The website encountered an unexpected error. Please try again later." page.
I have copied the maintenance-page.tpl.php file to my theme folder. and rendered a form in that template. Up until now the form is showing but when I submit the form, drupal is not getting into _validate and _submit functions.
Here is the code
Template File
<div class="complaint-form-message">
<h5>
We’re sorry you’re having trouble with myCCS!
Tell us about the error you’ve experienced and we’ll
get right back to you!
</h5>
</div>
<div class="complaint-form">
<?php echo drupal_render(drupal_get_form('complaint_form_form')); ?>
</div>
Form Function
function complaint_form_form($form, &$form_state){
$form['name'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'First Name & Last Name',
);
$form['email'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'Email Address',
);
$form['browser'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'What browser you are using?',
);
$form['tell_us_about_error'] = array(
'#type' => 'textarea',
'#required' => TRUE,
'#title' => 'Tell us about error you experienced?',
);
$form['attachment'] = array(
'#type' => 'file',
'#title' => 'Attachment',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => 'complaint_form_submit',
);
return $form;
}
function complaint_form_validate($form, $form_state){
echo 'PPPPP<pre>'; print_r($form_state); die;
$mail = $form_state['values']['submitted_tree']['email'];
if (!valid_email_address($mail))
form_set_error('[submitted][email_address]', t('The email
address appears to be invalid.'));
}
function complaint_form_submit($form, $form_state){
echo 'DDDDD<pre>'; print_r($form_state); die;
$values = $form_state['values'];
}
Your #submit is an array you have put is as string,
So change your submit entry to the following.
and place $form['submit'] to $form['actions']['submit'];
$form['actions']['submit']=array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('complaint_form_submit'),
);
Note: **'#submit' is an array**
to add validations
$form['#validate'][] = 'complaint_form_validate';
Note: '#validate' is an array
The purpose of the page is to show a list of jobs. The form at the top lets you filter the results. Once the form is submitted I want to show the form again as well as the values that were put in. How would I go about this in drupal. The form code is below.
function ac_resume_job_list_form($form, &$form_state)
{
$form['display_options'] = array(
'#type' => 'fieldset',
'#title' => 'Display Options',
'#attributes' => array("style" => "width:250px"),
);
$form['display_options']['limit'] = array(
'#type' => 'textfield',
'#title' => 'Limit',
'#size' => 2,
'#default_value' => 'the value of the submitted form',
);
$form['display_options']['submit'] = array(
'#type' => 'submit',
'#value' => 'Change Display',
);
return $form;
}
You'll want to set $form_state['rebuild'] = TRUE in the form submit handler, and basically you'll have access to all of the submitted values in the $form_state variable in the form when it's rebuilt:
$default_limit = isset($form_state['values']['limit']) ? $form_state['values']['limit'] : 0; // Or whatever default limit you want to have.
$form['display_options']['limit'] = array(
'#type' => 'textfield',
'#title' => 'Limit',
'#size' => 2,
'#default_value' => $default_limit
);
// Brief example for the submit form
function my_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}