Hi,

I am using webforms in Drupal 6. I am embedding the same form in different nodes using this code:

$node = node_build_content(node_load(80));
print drupal_render($node->content);
If the form validates, everything works great. If the form doesn't validate, it redirects to the node id of the form, not the parent form. I can't get the validation function to redirect to the parent node on error. I am saving the node id of the parent node as a hidden field on the form.

Comments

webdrips’s picture

First off, out of box, I recommend you use JQuery's most excellent validate library so you can avoid the problem altogether. However, I realize not everyone can depend on JS being available 100% of the time.

Assuming you're using the Webform module that allows for PHP code for both additional validation and additional processing, you can try something along these lines:

In your additional validation, let's say your checking for an error as follows:

  $error = array();
  if ($form_values['submitted_tree']['first_name'] == '') $error['error_first_name'] = 1;

You could then include the following to re-direct back to the source node and include any error messages from failed validation.

  if (count($error)>0) {
  $url = $_SERVER['HTTP_REFERER'];
  $result = strpos($url,'?');
  if ($result != FALSE) $url = substr($url, 0, $result);
  $msg = "error=1";
  $i = 0;
  foreach ($error as $key => $value) $msg .= "&".$key."=".$value;
  drupal_goto($url, $msg);
}

Finally, you could add some code in Webform markup fields to printout a message if any parameters were passed in the URL.

By the way, just in case you want to go to a nid after validation passes, do this (assuming you called your hidden field 'nid'):
$node->webform['confirmation'] = 'internal:' . $form_values['submitted_tree']['nid'];

Hopefully that at least gets you pointed in the right direction.

Regards,

Dan

Looking to Migrate from Drupal 6/7 to Drupal 10/11? Have a look at our Drupal 11 demo site and request access.

masterpiece’s picture

didn't see this, thanks man, by the way is there a way to get notified everytime someone answers my post on this site ? I'm new to drupal

webdrips’s picture

You'd think this would be possible (like on groups.drupal.org), but alas it is not.

Once logging in, I typically visit my "Your Dashboard" link to view recent activity.

Regards,

Dan

Looking to Migrate from Drupal 6/7 to Drupal 10/11? Have a look at our Drupal 11 demo site and request access.

cesargalindo’s picture

hook_form_alter(&$form, $form_state, $form_id) {

if (substr($form_id,0,20) == 'webform_client_form_') {

$form['#action'] = $_SERVER['REQUEST_URI'];

}

}

cesargalindo’s picture

hook_form_alter(&$form, $form_state, $form_id) {

  if (substr($form_id,0,20) == 'webform_client_form_')  {

      $form['#action'] = $_SERVER['REQUEST_URI'];

  }

}
tripper54’s picture

Thanks, that still works a treat with D7 / webform 3.x.

I'm using a form in a block, so failed validation needs to redirect to the current page.

------------------
Phil Dodd
twitter: @phildoddau

loopduplicate’s picture

When you aren't checking for any extra errors and you want the webform to redirect back to the same page that it's displayed on and still show messages when validation fails, for example when the webform is displayed as a block from a view, here is a modified snippet that you can place in the 'additional validation' section of webform 2.x:


// get array of errors
$error = form_get_errors();

// if errors exist display the error messages on the same page
if (count($error)>0) {
  $url = $_SERVER['HTTP_REFERER'];
  $result = strpos($url,'?');
  if ($result != FALSE) $url = substr($url, 0, $result);
  $msg = "error=1";
  foreach ($error as $key => $value) $msg .= "&".$key."=".$value;
  drupal_goto($url, $msg);
}