I am trying to add new fields based on the select list. I am using the following code
function my_action_plan_from($form, &$form_state) {
$form['#tree'] = TRUE;
$result = db_query('Select * from my_action_plan_settings ORDER BY action_type');
$records = array();
$i = 0;
foreach ($result as $record) {
$action_lists = array_merge($records, preg_split('/[\r\n]+/', trim($record->point_of_action), -1, PREG_SPLIT_NO_EMPTY));
$form[$action_name] = array(
'#type' => 'select',
'#options' => $action_lists,
'#attributes' => array('class' => array('enabled-for-ajax')),
'#ajax' => array(
'callback' => 'get_next_steps',
'wrapper' => 'item-action-'. $record->action_id,
'method' => 'append',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
return $form;
}
function get_next_steps($form, &$form_state) {
$newform['date'] = array(
'#type' => 'textfield',
'#size' => 20,
);
$newform['note'] = array(
'#type' => 'textarea',
);
$newform['percent'] = array(
'#type' => 'textfield',
);
return $newform;
}
It works fine with adding new fields based on selection, but the problem is the new fields added does not appear in $form_state. The values does not get submitted to submit handler, am i doing something wrong?
* MODIFIED CODE *
Maybe i was wrong at explaining, sorry, i have posted the modified code and yes i am creating the form in my_action_plan_form(). I am not trying to use $form_state for a permanent store, I was using it to create new fields in my_action_plan() but the problem is if i set a value $form_state['storage'] = "foobar" in get_next_step(), the value is not passed to my_action_plan(). $form_state['storage'] becomes empty in my_action_plan().
function my_action_plan_form($form, &$form_state) {
$form['#tree'] = TRUE;
$trigger_element = !empty($form_state['triggering_element']['#name']) ? $form_state['triggering_element']['#name'] : FALSE;
foreach ($result as $record) {
$action_lists = array_merge($records, preg_split('/[\r\n]+/', trim($record->point_of_action), -1, PREG_SPLIT_NO_EMPTY));
$action_name = "action_name_" . $record->action_id;
$form[$action_name] = array(
'#type' => 'fieldset',
);
if (empty($form_state['my_action_plan'][$action_name])) {
$form_state['my_action_plan'][$action_name] = array();
}
if($trigger_element && $trigger_element == $record->action_id ) {
if(!array_search($form_state['triggering_element']['#value'], $form_state['my_action_plan'][$action_name])) {
$form_state['my_action_plan'][$action_name][] = $form_state['triggering_element']['#value'];
}
}
$form[$action_name]['select'] = array(
'#type' => 'select',
'#options' => $action_lists,
'#attributes' => array('class' => array('enabled-for-ajax')),
'#ajax' => array(
'callback' => 'get_next_steps',
'wrapper' => $action_name . "-id",
),
);
if(count($form_state['my_action_plan'][$action_name]) > 0) {
for ($i=0; $i < count($form_state['my_action_plan'][$action_name]); $i++) {
$option = $form_state['my_action_plan'][$action_name][$i];
$form[$action_name][$option] = array(
'#type' => 'fieldset',
);
$form[$action_name][$option]['date'] = array(
'#type' => 'textfield',
'#size' => 20,
);
$form[$action_name][$option]['note'] = array(
'#type' => 'textarea',
);
$form[$action_name][$option]['percentage'] = array(
'#type' => 'textfield',
);
$submit_id = $action_name . '-' . $option . '-submit';
$form[$action_name][$option]['submit'] = array(
'#type' => 'fieldset',
);
$form[$action_name][$option]['submit'] = array(
'#type' => 'fieldset',
);
$form[$action_name][$option]['submit']['value'] = array(
'#type' => 'button',
'#value' => 'Save ',
'#ajax' => array(
'callback' => 'save_step',
'wrapper' => $submit_id,
),
);
}
}
return $form;
}
function get_next_steps($form, &$form_state) {
return $form[$form_state['triggering_element']['#parents'][0]];
}
Please help
Deepak
my_action_plan_form()
where the$form_state
can be used to determine what fields need to be created. Useget_next_steps()
only to set and rebuild theform_state
. Have a look at my answer for the similar question here