I want to show a Add more button that adds an extra text field and the corresponding Remove button.
I followed ajax_example_add_more()
to achieve this in Drupal 7. For Drupal 8, I tried with the following code.
class XmlfeedsSettingsForm extends FormBase {
public function getFormId() {
return 'formid';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Because we have many fields with the same values, we have to set
// #tree to be able to access them.
$form['#tree'] = TRUE;
$form['names_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('People coming to the picnic'),
// Set up the wrapper so that AJAX will be able to replace the fieldset.
'#prefix' => '<div id="names-fieldset-wrapper">',
'#suffix' => '</div>',
);
// Build the fieldset with the proper number of names. We'll use
// $form_state['num_names'] to determine the number of textfields to build.
$form_state->getValue['num_names_parent'] = '';
if (empty($form_state->getValue['num_names_parent'])) {
$form_state->getValue['num_names_parent'] = 1;
}
for ($i = 0; $i < $form_state->getValue['num_names_parent']; $i++) {
$form['names_fieldset']['name'][$i] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
}
$form['names_fieldset']['add_name'] = array(
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array('::ajax_example_add_more_add_one'),
// See the examples in ajax_example.module for more details on the
// properties of #ajax.
'#ajax' => array(
'callback' => '::ajax_example_add_more_callback',
'wrapper' => 'names-fieldset-wrapper',
),
);
if ($form_state->getValue['num_names_parent'] > 1) {
$form['names_fieldset']['remove_name'] = array(
'#type' => 'submit',
'#value' => t('Remove one'),
'#submit' => array('::ajax_example_add_more_remove_one'),
'#ajax' => array(
'callback' => '::ajax_example_add_more_callback',
'wrapper' => 'names-fieldset-wrapper',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
// This simply allows us to demonstrate no-javascript use without
// actually turning off javascript in the browser. Removing the #ajax
// element turns off AJAX behaviors on that element and as a result
// ajax.js doesn't get loaded.
// For demonstration only! You don't need this.
// if ($no_js_use) {
// Remove the #ajax from the above, so ajax.js won't be loaded.
// if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
// unset($form['names_fieldset']['remove_name']['#ajax']);
// }
// unset($form['names_fieldset']['add_name']['#ajax']);
// }
return $form;
}
public function ajax_example_add_more_add_one(array &$form, FormStateInterface $form_state) {
print_R($form_state);die;
//$form_state->getValue['num_names_parent']++;
//$form_state->setRebuild(TRUE);
}
public function ajax_example_add_more_callback(array &$form, FormStateInterface $form_state) {
return $form['names_fieldset'];
}
public function ajax_example_add_more_remove_one(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue['num_names_parent'] > 1) {
$form_state->getValue['num_names_parent']--;
}
$form_state['rebuild'] = TRUE;
}
print_r($form_state)
in buildForm()
outputs the following.
[getValue] => Array
(
[num_names_parent] => 1
)
In ajax_example_add_more_add_one()
, the num_names_parent index is missing.
How can I correctly add the Add more button?
WidgetBase.php
. If you need more help just ask. (there is no remove button, empty fields will be ignored).