Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kanagasabai on
I have built my form with a fieldset, which will rebuilt based on Add and Remove. The issue is I am unable to update the field's '#default_value' attribute, when a form is rebuilt by remove callback.
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
$i = 0;
$num_rows = $form_state->get('num_rows');
if (empty($num_rows)) {
$num_rows = $form_state->set('num_rows', 1);
}
if ($form_state->get('rem')){
$rem = $form_state->get('rem');
$counter1 = range(1, $num_rows);
$counter = array_flip($counter1);
unset($counter[$rem]);
$default = array_flip($counter);
$input = $form_state->getUserInput();
}
for ($i = 0; $i < $num_rows; $i++) {
if ($i == 0) {
$form['dep_row']['#prefix'] = '<div id="dep-row-wrapper" class="row">';
$form['dep_row']['#suffix'] = '</div>';
$form['field_add_dep'] = [
'#type' => 'submit',
'#value' => t('Add Dependent'),
'#submit' => array('::addOne'),
'#limit_validation_errors' => array(),
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'dep-row-wrapper',
],
'#attributes' => array('class' => array('add-dep-bt')),
];
}
if ($i > 0) {
$month_name = 'field_dob_dep_month_' . $i;
if (isset($default)) {
$m_default = 'field_dob_dep_month_' . $default[$i];
}
$form['dep_row'][$i] = [
'#type' => 'fieldset',
];
// Month
$form['dep_row'][$i][$month_name] = [
'#type' => 'textfield',
'#title' => t(''),
'#maxsize' => 2,
'#size' => 10,
'#maxlength' => 2,
'#placeholder' => t('MM'),
'#default_value' => ,
];
$form['dep_row'][$i]['actions']['remove_name_' . $i] = [
'#type' => 'submit',
'#value' => t('(remove_' . $i),
'#submit' => array('::removeCallback'),
'#limit_validation_errors' => array(),
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'dep-row-wrapper',
],
'#attributes' => array('class' => array('remove', 'dep-remove', 'dep-remove-' . $i))
];
}
}
}
public function removeCallback(array &$form, FormStateInterface $form_state) {
$triggerdElement = $form_state->getTriggeringElement();
$raw_element = explode('-', $triggerdElement['#attributes']['data-drupal-selector']);
$element = $raw_element[count($raw_element) - 1];
if (!in_array('spouse-remove' , $htmlIdofTriggeredElement)) {
$name_field = $form_state->get('num_rows');
if ($name_field > 1) {
$remove_button = $name_field - 1;
$form_state->set('num_rows', $remove_button);
$form_state->set('rem', $element);
}
$form_state->setRebuild();
}
}
?>
Comments
What's actually happening,
What's actually happening, and what is supposed to be happening?
More insight, please excuse my explaining inefficiency...
Suppose if textfield is added thrice through addmore callback and second element is being removed, i want to retain the inputvalue1 into-> textfield1 and inputvalue3 into-> textfield3.
inputvalue1
inputvalue2 //am removing this using remove button that calls the function removeCallback()
inputvalue3
this rebuilds the form, but the #default_values does not get updated accordingly in form rebuild as,
inputvalue1
inputvalue3
implemented in this page, with add dependent
I can't understand what you
I can't understand what you're saying that. Rather than referring to inputs etc, can you explain it in terms of the page you linked to? Also, the page you linked to does not work.
I have updated the page.
Updated link
When a user adds several dependents and removes a certain dependent, say dependent 2, i want to omit that dependent2 data alone and rebuild the form with other given data.