2

I have a form (loaded by obiumap function) that have a section loaded ( by function obiumap_addobj() ) with a $.ajax call from jQuery script (addobj.js) by clicking a button (addobj_btn). This new section is loaded in an

existing div (addobj-container) but without autocomplete functionality and also without default_value.

This is main form:

function obiumap($form, &$form_state) {
drupal_add_js(drupal_get_path('module', $this->modName) . '/addobj.js');
global $user;

$form=array();
$form['uid'] = array(
'#type' => 'hidden',
'#title' => 'uid',
'#default_value' => '3',
'#id' => 'uid',
);  
$form=array();
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'name',
'#id' => 'name',
);  
$form['addobj_btn'] = array(
     '#type' => 'button',
     '#value' => t('Add new objects'),
     '#id' => 'addobj_btn'
);  
$form['addobj-container'] = array(
        '#markup' =>  '<div id="addobj-container" ></div>'
);
die(drupal_render_children($form));
}

This is section definition loaded via ajax:

function obiumap_addobj($form, &$form_state){
global $user;
$form=array();
$form['uid'] = array(
'#type' => 'textfield',
'#title' => 'uid',
'#default_value' => '10',
'#id' => 'uid',
);  
$form['fldset_addobj'] = array(
'#type' => 'fieldset',
'#title' => t('Add new object'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['fldset_addobj']['objname'] = array(
'#type' => 'textfield',
'#id' => 'objname',
'#title' => t('Object name'),
'#autocomplete_path' => 'obimap/obiumap_addobj',
'#required' => TRUE,
);
 $form['fldset_addobj']['submit'] = array(
'#type' => 'submit',
'#value' => 'Add object',
'#id' => 'addobj_btn'
);
die(drupal_render_children($form));
}   

and this is addobj.js

function addItems(arg1, arg2) {
jQuery(document).ready(function() {
jQuery('#addobj_btn').click(function() {  
var formURL = 'obiumap_addobj';
jQuery("#obiumap").submit(function(){
 return false;
 });
    jQuery.ajax(
    {
        url : formURL,
        type: "GET",
        data : { uid: jQuery('input[name=uid]').val() },
        success:function(data, textStatus, jqXHR)
        {
            jQuery('#addobj-container').html(data);
            console.log("###### Drupal.attach #####");
            Drupal.attachBehaviors("#addobj-container");
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
        }
    });

});

});
}

I really need autocomplete for that field but I don't now how to activate it.

0

If you want Drupal's autocomplete, you need to modify form using Form API '#ajax'. If you are doing it with your custom scripts, Form API will not know that field really belongs to your form, and will not handle field's calls.1

The way you should do it is:

  1. In your form building function, create element with '#ajax' property, that will be responsible for loading your form part, or add it to existing element.

  2. Also in your form building function, add an if that will check triggering element ($form_state['triggering_element']), and if that was your button, add entry in $form_state that new section should be visible.

  3. If there is an entry mentioned in 2., output form part you want to be loaded with AJAX.

  4. Use AJAX callback function only to select and return proper part of the form.


1 Simplified explanation, of course.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.