Add more fieldset with remove option custom module

Created on 14 April 2016, about 8 years ago
Updated 16 February 2023, over 1 year ago

I'm developing custom module and there's one fieldset that can be added multiple times and can be removed any particular entry also. Can anyone please suggest to achieve this? Here is my code:

function hook_form($form,&$form_state){
$form_state['storage']['emails'] =
isset($form_state['storage']['emails']) ? $form_state['storage']['emails'] : 0;

$form['emails'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div id="emails">',
    '#suffix' => '</div>',

);
if(empty($form_state['storage']['emails'])) {
    $form_state['storage']['emails'] = 0;
}
for ($j = 0; $j <= $form_state['storage']['emails']; $j++) {
    $form['emails'][$j]['candidate_email'] = array(
        '#title' => 'Email',
        '#type' => 'textfield',
        '#size' => '30',
        '#required' => FALSE,
    );
}
$form['emails']['add_email'] = array(
    '#type' => 'button',
    '#value' => t('Add Email'),
    '#href' => '',
    '#ajax' => array(
        'callback' => 'hook_ajax_add_emails',
        'wrapper' => 'emails',
    ),
);
$form_state['storage']['emails']++;

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',        
);
return $form;
}

function hook_ajax_add_emails($form, $form_state) {
    return $form['emails'];    
}
💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India Virang

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024