Posts Tagged form

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

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

Saving node’s fields without saving the node itself

Drupal core function node_save() is used to save nodes and it is frequently used to programmatically alter and save nodes’ data, which includes fields (aka CCK fields) as well. If you want to update a field of a node you could do it like this,

<?php
$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;
node_save($node);
?>

However, there may be a case when you might want to save a node’s field, but not the node itself. For example, you may not want to update the node’s timestamp for the last update, because you want it to be changed by human user activity only. In another case, you may not want to call node_save() and thus invoke chain reactions such as email notifications when the node is updated. These two cases are what actually happened to me.

A Drupal function field_attach_update() may save your day if you encountered a similar problem that I had. You could actually replace node_save() with field_attach_update(),

<?php
$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;
field_attach_update(‘node’, $node);
?>

,

Leave a comment

Floating a textfield next to a radio button in Drupal form

This can actually be done entirely with the Forms API. Here is an example:

 

// This will mimic the normal form element title as well as provide the group for our radio options.
$form[‘nations’] = array(
‘#type’ => ‘item’,
‘#title’ => t(‘Nationality’),
‘#description’ => t(‘<Enter your field description here.>’)
);
$form[‘nations’][‘american’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘American’),
‘#default_value’ => 1,                    // If there is a default value, this should also be specified for each radio button.
‘#return_value’ => 1,
‘#parents’ => array(‘nations’),           // You must specify this for each radio button for them to act as a group.
‘#prefix’ => ‘<div>’  // The first radio button needs to make the style match a normal radios group.
);
$form[‘nations’][‘german’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘German’),
‘#return_value’ => 2,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
);
$form[‘nations’][‘other’] = array(
// The ‘container-inline’ class places elements next to each other, while the ‘form-item’ class provides the correct spacing between options.
‘#prefix’ => ‘<div>’,
‘#suffix’ => ‘</div>’
);
// By supplying the title here, instead of using the ‘#field_prefix’ property of the textfield, clicking the text will also select the radio button.
$form[‘nations’][‘other’][‘other_option’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘Other:’),
‘#return_value’ => 3,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
);
$form[‘nations’][‘other’][‘other_textfield’] = array(
‘#type’ => ‘textfield’,
‘#default_value’ => ”,
‘#size’ => 20,         // The default size is a bit large…
‘#suffix’ => ‘</div>’  // End of the “form-radios” style.
);

In your validation and/or submit callbacks, the selected radio option will then be in $form_state['values']['nations'] and the textfield value will be in $form_state['values']['other_textfield'].

With a little more work, you can change the example so that you don’t have to create each element manually .-)

, ,

Leave a comment