Archive for category From API

SOLVED!! How can you make form_set_error() affect the only form submitted?

Drupal form_set_error() function provide validation message based field name. So if you have same field name in same page , after the submission you can see both fields are marked as red color(Error class). It validate or show message for right form but it provide error class for all element which are same.

I found a way to solve it. If you render those form using drupal_get_form then you can add one extra line in top to get rid of this problem.

for example :-

drupal_static_reset('form_set_error'); // adding this line makes you life out of hell.-)
$form = drupal_get_form('webform_client_form', $node, $submission);
return drupal_render($form);

Ref:- http://drupal.stackexchange.com/questions/29113/how-do-i-make-form-set-error-affect-the-submitted-form-only

,

Leave a comment

Drupal webform/custom form render multiple times on same page

I needed to place the same form multiple times on a drupal page but still be unique so that FAPI would know which one was being submitted.

To do that, first thing we need to render the same form in our function with non-existing form_id as follow:-

————

$form = drupal_get_form(‘webform_client_form_’ . $webform_nid . ‘_’ . $nid, $node);
drupal_render($form);

* it means ‘webform_client_form_’ . $webform_nid . ‘_’ . $nid  form_id does not exists in the Drupal site. 

————

Then we need to define hook_forms function in module.

function hook_forms($form_id, $args) {
$forms = array();
if (strncmp($form_id, ‘webform_client_form_’, 20) === 0) {
$forms[‘webform_client_form_’ . $args[0]->nid] = array(‘callback’ => ‘webform_client_form_’ . $args[0]->nid);
}
return $forms;
}

This is an example for same webform rendering multiple times in same page. It will work for custom form as well. It will also do the trick to track which form is submitted.  It will do validate only the form where button is clicked.

,

Leave a comment

Disable form validation on ajax call in Drupal form

If you use multiple submit button in Drupal from and add ajax call to submit button then you ajax submit button will execute form default validate and submit function. To prevent it from calling those default function you just need to do two following steps.

1) rename my validation function from “myformname_validate” to “myformname_do_validate”. This caused my validate function to not be called during the ajax request.

2) attach the “myformname_do_validate” function to my submit button like so:

<?php
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Submit’),
‘#validate’ => array(‘myformname__do_validate’),
);
?>

, ,

Leave a comment