2

I have a custom form that uses AJAX to add new fieldsets

  1. internal users can specify a default set of artists, e.g. 4, and send this to a record label/management etc.

  2. the rep can come to the form, and add additional fields (via ajax)

After specifing some of the fields that are part of the AJAX fieldsets to '#required' , the values are no longer in form_state['values'] but still show in form_state['input']

The question is, how do I send the values in $form_state['input'] into $form_state['values'] to take advantage of the drupals form processing?

function quickmusic_form($form, &$form_state, $this_node = false) {
// Set default values, if any
if(!empty($this_node->field_heading_text['und'][0]['value'])){
    $default_heading = $this_node->field_heading_text['und'][0]['value'];
}else{
    $default_heading = return_default_heading('quickmusic');
}

if(!empty($this_node->field_paragraph_text['und'][0]['value'])){
    $default_paragraph = $this_node->field_paragraph_text['und'][0]['value'];
}else{
    $default_paragraph = return_default_paragraph('quickmusic');
}

if(!empty($this_node->field_first_name['und'][0]['value'])){
    $default_first = $this_node->field_first_name['und'][0]['value'];
}else{
    $default_first = '';
}

if(!empty($this_node->field_last_name['und'][0]['value'])){
    $default_last = $this_node->field_last_name['und'][0]['value'];
}else{
    $default_last = '';
}

if(!empty($this_node->field_quickform_type['und'][0]['value'])){
    $default_tag = $this_node->field_quickform_type['und'][0]['value'];
}else{
    $default_tag = 'QuickMusic';
}

$form = array();
$form['#tree'] = TRUE;
$_SESSION["anonymous-tracker"]=time();
$form['#attributes']['enctype'] = 'multipart/form-data';
// Add module form css
$form['#attached']['css'] = array(
    drupal_get_path('module','quickform') . '/css/form.css',
);
// Add module form js
$form['#attached']['js'] = array(
    drupal_get_path('module','quickform') . '/js/form.js',
);

//Text
$form['headingtext1'] = return_heading_1($default_heading);
$form['headingtext2'] = return_heading_2($default_paragraph);
$form['headingtext3'] = return_heading_3();

//Contact
$form['contact_info']= return_contact_fieldset();
$form['contact_info']['first_name'] = return_first_name($default_first);
$form['contact_info']['last_name'] = return_last_name($default_last);
$form['contact_info']['email1'] = return_email1();

//Artist
$form['artist_info'] = return_artist_wrapper();
$form['artist_info']['tos_c'] = return_tos_c();
$form['artist_info']['tos_p'] = return_tos_p();
$form['artist_info']['tags_c'] = return_tags_c($default_tag);

// artists_count =  added by internal user, defaults to 1
// next_artists = for end user to add more
// if artist count is empty, set it to 1
// if internal artists is not empty, set it to the count of total internal artists

if(empty($form_state['artists_count'])){ 
    $form_state['artists_count'] = 1;
    $form_state['ajax_artists'] = 1;
    $form_state['next_artist'] = $form_state['ajax_artists'] + 1;       

    if (!empty($this_node->field_artist_names['und'][0])){
        $form_state['artists_count'] = count($this_node->field_artist_names['und']);
        $form_state['ajax_artists'] = count($this_node->field_artist_names['und']);
        $form_state['next_artist'] = $form_state['ajax_artists'] + 1;
    }
}

// This controls the # of artists added internally
// Lets add either 1 artist (default)
// Or X number of artists as determined by artist names via node create form
$total_artists = $form_state['artists_count'];

for ($x = 0; $x < $total_artists; $x++){
    // Grab node information
    if(!empty($this_node->field_artist_names['und'][$x]['value'])){
            $this_artist = $this_node->field_artist_names['und'][$x]['value'];
    }else{
        $this_artist = '';
    }

    //each artist requires a set of fields
    $form['artist_info'][$x] = return_artist_container($x);
    $form['artist_info'][$x]['artist'] = return_artist_fieldset_loop();
    $form['artist_info'][$x]['artist']['account_name'] = return_artist_name($this_artist, $x, true);
    $form['artist_info'][$x]['artist']['music_text'] = return_artist_music_text();
    $form['artist_info'][$x]['artist']['titles'] = return_artist_music_titles();
    $form['artist_info'][$x]['artist']['music'] = return_artist_music();
    $form['artist_info'][$x]['artist']['remove'] = return_rem_artists($x);

}#end artist_count loop

$change_artist = $form_state['ajax_artists'];

// if next artist == ajax artists, replace the hidden artist with a visible artist // ajax artists only == ajax_artists AFTER the add more button is clicked if($form_state['next_artist'] == $form_state['ajax_artists']){ $form_state['rebuild'] = TRUE; // get the current # // ajax artists are incremented on button click $x = $form_state['next_artist']; $form_state['next_artist']++;

//each artist requires a set of fields
$form['artist_info'][$x] = return_artist_container($x);
$form['artist_info'][$x]['artist'] = return_artist_fieldset_loop();
$form['artist_info'][$x]['artist']['account_name'] = return_artist_name('', $x, true);
$form['artist_info'][$x]['artist']['music_text'] = return_artist_music_text();
$form['artist_info'][$x]['artist']['titles'] = return_artist_music_titles();
$form['artist_info'][$x]['artist']['music'] = return_artist_music();
$form['artist_info'][$x]['artist']['remove'] = return_rem_artists($x);

}#end if statement

// Add another file button // When clicked, it will replace hidden artist with a visible artist $form['add_more_artists'] = return_add_artists($change_artist);

$form['submit'][] = return_form_submit(); $form['#submit'][] = 'quickjoin_form_submit';

echo '
', print_r($form_state), '
';

return $form; } #end quickmusic form

//This wraps the entire artist section function return_artist_wrapper(){ return array( '#type'=>'fieldset', '#title'=>t('Your Artists'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, ); }

//this wraps each artist function return_artist_container($x = 0){ return array( '#type' => 'container', '#prefix' => "", #should be different for each artist '#suffix' => '', '#tree' => TRUE, ); }

//This is for the default set of artists && the AJAX function return_artist_fieldset_loop($x =0){ return array( '#type'=>'fieldset', '#title'=>t('Artist'), #'#description'=>t('All fields are required'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); }

// Add more button function return_add_artists($change_artist){ return array( '#type' => 'submit', '#name' => "add_more_artists", '#value' => t('Add Another Artist'), '#submit' => array('artists_add_more'), #increment $form_state['ajax_artists'] '#ajax' => array( 'callback' => 'artists_add_more_callback', #function that returns elements 'wrapper' => "artists-wrapper", #method this wrapper with the returned elements 'method' => 'append', #May be: 'replaceWith', 'append', 'prepend', 'before', 'after', or 'html' ), ); }

/** * Submit handler for the "add-one-more" button. * Increments the max counter and causes a rebuild. * This gets called before the call back */ function artists_add_more($form, &$form_state) { //dont increment if there are errors on the form if (form_get_errors()) { return false; }else{ $form_state['ajax_artists']++; $form_state['rebuild'] = TRUE;
} }

/** * select and return the portion of the form * that is to be replaced on the original page. */ function artists_add_more_callback($form, &$form_state) { if (form_get_errors()) { return false; }else{ $this_artist = $form_state['ajax_artists']; $form_state['rebuild'] = true; return $form['artist_info'][$this_artist]; } }

function artists_rem_more($form, &$form_state) { //dont increment if there are errors on the form $form_state['rebuild'] = TRUE;
}

function return_rem_artists($this_artist){ return array( '#type' => 'button', '#value' => t('X'), #'#submit' => array('artists_rem_more'), #increment $form_state['artists_count'] '#ajax' => array( 'callback' => 'return_rem_artist', #function that returns elements 'wrapper' => "artist{$this_artist}", #replace this wrapper with the returned elements 'method' => 'replaceWith', #May be: 'replaceWith', 'append', 'prepend', 'before', 'after', or 'html' ), ); }

/** *this gets replaced via the add more callback */ function return_hidden_artist($change_artist){ return array( '#type' => 'hidden', '#name' => "artist{$change_artist}", '#prefix' => "", #should be incremented '#suffix' => '', ); }

function return_rem_artist(){ return array( '#type' => 'hidden', '#name' => "deleted", ); }

Your Answer

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

Browse other questions tagged or ask your own question.