I have created a multiupload field in my node using Multiupload Filefield Widget. I am using the code below to alter the default node form.
There is an ajax based dependent dropdown field in the form i.e city dependent on country.When I select a single country the form work fine. But when I select multiple values form the country list I get the follwing error:
An illegal choice has been detected. Please contact the site administrator.
I get this particular error when there is the multiupload form element present. When I remove the element from $form its works fine with multiple selected values.
Please help me out to solve this problem.Thanks in advance!
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'mynode_node_form':
$country_list = load_countries();
$selected_country = (isset($form_state['values']['country'])) ? $form_state['values']['country'] : key($country_list);
$form['countries'] = array(
'#type' => 'select',
'#title' => t('Select Your Country'),
'#options' => $country_list,
'#default_value' => $selected_country ,
'#ajax' => array(
'callback' => 'city_dropdown_callback',
'wrapper' => 'city_wrapper',
),
'#multiple' => TRUE,
'#required' => TRUE,
);
$cities_list = load_cities($selected_country);
$selected_cities = (isset($form_state['values']['cities'])) ? $form_state['values']['cities'] : key($cities_list);
$form['cities'] = array(
'#type' => 'select',
'#title' => t('Select Your City'),
'#prefix' => '<div id="city_wrapper">',
'#suffix' => '</div>',
'#options' => $cities_list,
'#default_value' => $selected_cities,
'#multiple' => TRUE,
'#required' => TRUE,
);
}
}
function city_dropdown_callback($form,$form_state){
return $form['cities'];
}
Related
I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.
The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)
Would anyone know what I am doing wrong here? I cannot see how my stuff differs.
On my .admin.inc file I have set the form as such:
function contact_page_settings() {
$form = array();
$config = contact_page_default_settings();
$form['#tree'] = TRUE;
$form['contact_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Top Section'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'top-title' => array(
'#type' => 'textfield',
'#title' => t('Top title'),
'#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
),
'top-left' => array(
'#type' => 'text_format',
'#title' => t('Top left'),
'#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
),
'top-right' => array(
'#type' => 'text_format',
'#title' => t('Top right'),
'#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
),
);
return system_settings_form($form);
}
On my .module file I have:
function contact_page_menu() {
$items = array();
$items['admin/settings/contact-page'] = array(
'title' => 'Contact Page content',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('contact_page_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => '/admin/contact_page.admin.inc',
);
return $items;
}
function contact_page_default_settings() {
$defaults = array(
'top-tile' => 'Top title',
'top-left' => 'Top left',
'top-right' => 'Top right',
);
$config = variable_get('contact_page_settings', array());
return array_merge($defaults, $config);
}
OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.
The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.
So, I just added ['value'] to the default_value ternary expression:
'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',
I am building a form using the Drupal 7's Form API... all has been kosher thus far; but looks like I have hit a wall. Looking for guidance/point in the right direction:
A user logs in using CAS (Central Authentication Service). Clicks on a form to create a new record.
At the top of the form, I have a checkbox (which is checked by default), followed by three fields: ID, Name, Phone, and Email.
I pre-populate these fields using the following code:
<?php
//... other code prior to this function
function user_form_form_start($form, &$form_state) {
global $user;
$result = db_query('select u.name, dn.field_full_name_value, pn.field_phone_value, u.mail from field_data_field_full_name dn join field_data_field_phone pn on (dn.entity_id = pn.entity_id) join users u on (pn.entity_id = u.uid) where u.uid = :uid', array(':uid' => $user->uid))->fetchAll();
$myID = isset($result[0]->name) ? $result[0]->name : "";
$myFullName = isset($result[0]->field_full_name_value) ? $result[0]->field_full_name_value : "";
$myPhoneNumber = isset($result[0]->field_phone_value) ? substr($result[0]->field_phone_value, 2) : "";
$myEmailAddress = isset($result[0]->mail) ? $result[0]->mail : "";
$form['tab_user'] = array(
'#type' => 'fieldset',
'#title' => t('User Details'),
'#collapsible' => TRUE, // Added
'#collapsed' => FALSE, // Added
);
$form['tab_user']['user_sameAsAuthor'] = array(
'#type' => 'checkbox',
'#title' => t('Filling it out for myself'),
'#default_value' => isset($form_state['object']->user_sameAsAuthor) ? $form_state['object']->user_sameAsAuthor : TRUE,
// '#required' => TRUE,
);
$form['tab_user'] ['user_id'] = array(
'#type' => 'textfield',
'#title' => t('My CAS ID'),
'#default_value' => isset($form_state['object']->user_id) ? $form_state['object']->user_id : $myID,
// '#required' => TRUE,
);
$form['tab_user']['user_name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($form_state['object']->user_name) ? $form_state['object']->user_name : $myFullName,
// '#required' => TRUE,
);
$form['tab_user']['user_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number'),
'#default_value' => isset($form_state['object']->user_phone) ? $form_state['object']->user_phone : $myPhoneNumber,
'#attributes' => array('class' => array('phoneNumberField')),
'#size' => 20,
// '#required' => TRUE,
);
$form['tab_user']['user_email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#default_value' => isset($form_state['object']->user_email) ? $form_state['object']->user_email : $myEmailAddress,
// '#required' => TRUE,
);
return $form;
}
//... other code after this function
?>
ALL of the above works beautifully! Here's where I am stuck:
If the user unchecks the check box, I would like the user to input a new ID. Based on whatever ID user inputs, how can I "ajaxically" fetch Name, Phone, and Email doing an LDAP lookup?
Any and all help greatly appreciated!
EDIT:
Apologies! Forgot to mention that in my research, I dug through the Form API (of course) and noticed that textfield (https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#textfield) accepts ajax property (https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#ajax). However, the example code does not fully answer my question.
i want to pass drop down value in ajax-callback function as argument ($user_id,$selected) onchange but i am not getting $selected value in this ajax-callback function onchange event**
use jQuery: $("#yourdropdownid option:selected").text();
$form['changethis'] = array(
'#type' => 'select',
'#title' => t('Rece/Paid'),
'#description' => t('See Records'),
'#options' => $options,
'#default_value' => $selected,
'#ajax' => array(
'callback' => 'ajax_callback',
'wrapper' => 'replace_textfield_div',
'event' => 'change',
'progress' => array( 'type' => 'none', ),
),
);
Above added for readability.
Is your select posting something and not getting a result? Or is it not posting anything at all?
Your code looks ok, but are you sure you want ajax callback 'ajax_callback'? Shouldn't this be your own function that returns part of the form?
Also, I'm not sure about 'progress' => array( 'type' => 'none', ), The documentation says only 'throbber' and 'bar' are valid.
Try something like this instead
function my_module_create_form($form, $form_state){
// check form_state here to see if the form has been submitted
// via ajax and change if needed.
$options = array(0, 1, 2);
$selected = 0;
$form = array();
$form['changethis'] = array(
'#type' => 'select',
'#title' => t('Rece/Paid'),
'#description' => t('See Records'),
'#options' => $options,
'#default_value' => $selected,
'#ajax' => array(
'callback' => 'my_module_ajax_callback',
'wrapper' => 'replace_textfield_div',
'event' => 'change',
),
);
return $form;
}
function my_module_ajax_callback($form, $form_state) {
// because this is called from change event on select,
// it wont rebuild the form (call my_module_create_form).
// If you want it to you have to set $form_state['rebuild']
// (or it may be $form['rebuild'], I can't remember which one.)
// return form part
return $form['changethis'];
}
I am struggling in my attempts to programmatically display a form to display in my node-page view area. I have the following code in my "simplemodule".
function simplemodule_newcomer_form($form_state){
$form = array();
$form['simplemodule_newcomer']['name'] = array(
'#type' => 'textfield',
'#title' => t('name'),
'#description' => t(''),
'#weight' => -1,
);
$form['simplemodule_newcomer']['email'] = array(
'#title' => t('email'),
'#type' => 'textfield',
'#description' => t(''),
'#weight' => 0,
);
$form['simplemodule_newcomer']['phone'] = array(
'#title' => t('telephone No.'),
'#type' => 'textfield',
'#description' => t(''),
'#weight' => 0,
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => 'enter',
);
return $form;
}
function simplemodule_newcomer_form_submit($form_id, &$form_state){
//dealing with submitted data
}
This code works but only from a defined link in my administration menu.
What I want to do is get the form to display and submit on a specific node in view mode. So it creates the effect that there is a form to fill when visiting the node.
You can implement hook_nodeapi() and attach your form using drupal_get_form():
function simplemodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->nid == $the_nid && $op == 'view') {
$node->content['my_additional_field'] = array(
'#value' => drupal_get_form('simplemodule_newcomer_form'),
'#weight' => 10,
);
}
}
You can use the #weight key to specify where in relation to the other content on the page your form will appear. Also you'll need to clear Drupal's caches when you implement this hook to make sure it's picked up.
Surely you can use
hook_form_alter(&$form, &$form_state, $form_id) and
hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7
http://drupal.org/node/1011692
I've a form in my module (mymodule)...
function mymodule_form()
{
$form['mytext'] = array(
'#title' => 'Password',
'#type' => 'password',
'#size' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Cool!',
);
$form['cool_submit'] = array(
'#type' => 'submit',
'#value' => 'Cool Submit!',
);
return $form;
}
I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$entity->content['myadditionalfield'] = mymodule_form();
}
When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?
Thanks :-)
I worked further on this question to solve it. And the problem got solved. I changed the above code...
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$element = array(
'start' => array(
'#type' => 'button',
'#name' => 'start',
'#button_type' => 'submit',
),
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
),
'my_submit' => array(
'#type' => 'button',
'#name' => 'Submit Discussion',
'#button_type' => 'submit',
),
);
$entity->content['disc_bar'] = $element;
}
And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
'#theme-wrapper' => '',
),
and It worked. Its not generating any additional division tags... :-)