Reputation: 1812
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.
Upvotes: 1
Views: 78