Simple form slide
| Project: | AHAH Forms Framework |
| Version: | 5.x-1.5-5 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Hi,
I think the Ahah forms module is just what I need to create form elements that can be hidden or shown by clicking buttons. I'm trying to do this by amending the "todos" module, deleting everything I don't need. However, it's not working as expected.
I've made a test module called "my_module" (zip file here) that has a database table with four columns: 'nid', 'vid', 'first_field', and 'second_field'. I want the form fields for 'first_field' and 'second_field' to appear in the page only if the user clicks a button (and to disappear if another button is clicked). The 'my_module_node_form' has two fields:
<?php
/**
* Implementation of hook_form().
*/
function my_module_form(&$node) {
$type = node_get_types('type', $node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
// subform wrapper
$form['items'] = array (
'#type' => 'item',
'#theme' => 'my_module_subform',
'#ahah_bindings' => array(
array (
'wrapper' => 'my_module_wrapper'
),
),
);
// actually build the widget and put it in the wrapper
if( module_exists ('ahah_forms') ) {
$form['subform'] += dynamic_subform_get_embedded('my_module_node_form', 'my_module_subform', $node);
}
else { // for non-ahah
$form['subform'] += my_module_subform ($node);
}
return $form;
}
?>The subform looks like this:
<?php
function my_module_subform($node, $form_values=null, $pass=null) {
if( is_array($node)) drupal_set_message("my_module_subform: missing node!");
if( !empty($form_values) && !is_array($form_values)) drupal_set_message( "my_module_subform: bad form_values" );
$form['mybutton'] = array (
'#type' => 'button',
'#value' => 'Display subform',
'#ahah_bindings' => array (
array(
'wrapper' => 'my_module_wrapper',
'event' => 'click',
'path' => 'my_module/my_module_update_js',
),
),
);
$form['mybutton2'] = array (
'#type' => 'button',
'#value' => 'Remove subform',
'#ahah_bindings' => array (
array(
'wrapper' => 'my_module_wrapper',
'event' => 'click',
'path' => 'my_module/my_module_update_js',
),
),
);
if($form_values['op'] == 'Display subform') {
$form['first_field'] = array (
'#title' => 'First field',
'#type' => 'textfield',
'#default_value' => $node->first_field,
'#required' => TRUE
);
$form['second_field'] = array (
'#title' => 'Second field',
'#type' => 'textfield',
'#default_value' => $node->second_field,
'#required' => TRUE
);
}
if($form_values['op'] == 'Remove subform') {
unset($form['first_field']);
unset($form['second_field']);
}
return $form;
}
?>This works OK. The subform gets added to the form when the user clicks the "Display subform" button and disappears when the "Remove subform" button is clicked. However, the two fields do not get added to the $node. If I remove the conditional (if($form_values['op'] == 'Display subform')), the fields are added to the node and are available as $node->first_field and $node->second_field, but that's not much use because in that case they're always on display. Have I missed something simple or have I completely missed the point?
Also, I noticed something that doesn't seem right in the "todos" module. If I add a few todos, click "Preview", and then attempt to add another todo, I get the error message:
- warning: array_merge_recursive() [function.array-merge-recursive]: recursion detected in C:\xampp\htdocs\includes\common.inc on line 1674.
- form_id mismatch: todos_node_form vs : No cached subform_build_id! : Can not find old cached args : Bad subform_id in args - vs todos_items_subform
Any idea why?
Cheers,
Jeff

#1
Sorry, the line below
<?php// subform wrapper
?>
should be
<?php$form['subform'] = array (
?>
not
<?php$form['items'] = array (
?>
Cheers,
Jeff
#2
I've also run into the recursion detected error/warning when trying to build code based on the todo module. Haven't figured out the cause yet.
#3
the recursion bug report seems like a duplicate of http://drupal.org/node/151712
#4
Same problem here. Tried a bit of hacking the dynamic_forms module, but couldn't figure out the cause of this issue. Subscribing.
#5