Hello,

I'm writing a module that will alter my form for one of my node types (tip). It effectively is meant to change the copy for the submit button and have the form be processed using AJAX so the interaction will remain in the colorbox that the node add form has opened in.

My code is below. Everything appears to work meaning:
1. the button copy is correct
2. if a user forgets a required field then the user is notified and the fields are highlighted all while staying in the colorbox
3. after submitting the form with all required fields completed then the user gets the appropriate "Thank you" message.

What doesn't work is the node never actually gets saved and does not appear in the content listing - any advice would be greatly appreciated!

function simone_forms_form_alter(&$form, &$form_state, $form_id) {
    //dsm($form_id); // to see form ID
    if ($form_id == "tip_node_form") {
$form['actions']['submit'] = array(
	'#type' => 'submit',
	'#ajax' => array(
		'callback' => 'simone_forms_ajax_callback',
		'wrapper' => str_replace('_','-',$form['#form_id']),
		'effect' => 'fade',
	),
	'#value' => t('Super Submit')
);
$form['#submit'][] = 'simone_forms_ajax_form_submit';
    }
}

function simone_forms_ajax_form_submit(&$form, &$form_state) {
	$form_state['rebuild'] = FALSE;
}

function simone_forms_ajax_callback(&$form, &$form_state) {
	if(form_get_errors()){
		return $form;
	}
	else{
		return 'Thank You';
	}
}

Comments

blacklabel_tom’s picture

I would have a look at the AJAX example in the examples module:

http://drupal.org/project/examples

Off the top of my head simone_forms_ajax_form_submit should act like a normal form submit handler and the node updating and saving should live in there.

Cheers

Tom

TimToronto’s picture

Thank you Tom - I just downloaded the example you referenced and it seems to all be in there, I'm going to give it a shot now.

TimToronto’s picture

With help from the ajax_example module I was able to get my form to work, here is my final code in case anyone has the same issue:

function simone_forms_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "tip_node_form") {
	$form['actions']['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('Submit Your Tip'),
	    '#submit' => array('node_form_submit'),
		'#ajax' => array(
		       'wrapper' => str_replace('_','-',$form['#form_id']),
		       'callback' => 'simone_forms_ajax_callback',
		       'method' => 'replace',
		       'effect' => 'fade'
		   )
	  );
}
}

function simone_forms_ajax_callback($form, &$form_state){
  	if(form_get_errors()){
		return $form;
	}
	else{
		return 'Thank you';
	}
}
anybody’s picture

The solution from TimToronto works absolutely great!
One problem I ran into when using it was the following error:
Call to undefined function node_form_validate() ...

I wrote a blog article about my solution to that problem which may help others if you run into the same problem:
http://julian.pustkuchen.com/en/solution-call-undefined-function-nodefor...

As you can see the implementation is based on TimTorontos solution specifically.

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes