Drupal Commerce custom checkout pane state not working as expected - drupal

I'm attempting to add a conditionally displayed fieldset in a custom checkout pane. The fieldset is always displayed regardless of the state, unlike on a standard form.
For example, in the code below I want the "hungry_fields" fieldset displayed based on the value of the "hungry" radio button.
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['hungry'] = array(
'#type' => 'radios',
'#options' => array('yes' => t('Yes'), 'no' => t('No')),
'#required' => TRUE,
'#title' => t('I am hungry')
);
$pane_form['hungry_fields'] = array(
'#title' => 'Hungry',
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name="hungry"]' => array('value' => 'yes'),
),
),
);
return $pane_form;
}
I'm new to Drupal Commerce so it's entirely possible I'm missing something.

I fixed this by using a form callback with drupal_get_form() instead of adding the fields to the array directly.
For example:
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['form'] = drupal_get_form('hungry_form');
}
function hungry_form($form, &$form_state) {
...
}
Using this method the forms work properly.

Related

D6: drupal_render in form causes various problems (default value, ID, date_select)

I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).
I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.
The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
Here are the problems I am encountering:
ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).
Let's hope that's it =)
Can anybody help? What am I doing wrong?
A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.
Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.
Stupid me =)

Why is the drupal 6 status message not showing what fields are required when I first submit the form

When I submit the form without filling in one of the required fields( or any combination of required fields) there is no status message presented to let me know I have missed the required fields.
The second time I submit the form the status message shows me what fields are required.
The status message seems to be one step behind the form submission.
If after the first submit I change what fields are filled in and I submit again then the status message that should have show the previous time will show now.
When the form is filled in correctly it submits as normal.
The form is displayed using drupal_get_form( 'otherWaysToRequest' );
This is called in a template file in the theme.
Does anyone know why the status message is one step behind?
This is a sample of the code being used
function otherWaysToRequest(&$form_state)
{
global $base_url;
$pathToTheme = path_to_theme();
$form['top-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
);
$form['top-check']['gift'] = array(
'#title' => t('Included a gift'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['top-check']['contact'] = array(
'#title' => t('I would like to speak to you'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => true,
);
$form['email'] = array(
'#title' => t('Email Address'),
'#type' => 'textfield',
'#required' => true,
);
$form['bottom-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
'#description' => t('<p class="Items">If you have ...:</p><p class="Items">I have included .....</p>')
);
$form['bottom-check']['share'] = array(
'#title' => t('A Share'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'image_button',
'#src' => $pathToTheme.'/image.gif',
'#value' => t('Submit Form'),
);
}
function otherWaysToRequest_validate($form, &$form_state)
{
$mail_reg_ex = '/[-a-zA-Z0-9._]+[#]{1}[-a-zA-Z0-9.]+[.]{1}[a-zA-Z]{2,4}/';
if(!preg_match($mail_reg_ex, $form_state['values']['email']))
{
form_set_error('email', t('Invalid email address.'));
}
if( 0 == $form_state['values']['gift'] & 0 == $form_state['values']['contact'] )
{
form_set_error('gift', t('You must choose one of the first two options on the form'));
}
}
function otherWaysToRequest_submit($form, &$form_state)
{
//mail details
}
It's because by the time you're calling drupal_get_form in your template file the messages have already been committed for the current page; your validation messages will therefore show up the next time messages are displayed to the screen which is on the next page load.
You should build up the form in a custom module rather than the theme to get around this. The easiest way would be to create a block which you can assign to a region (using either hook_block in Drupal 6 or a combination of hook_block_info() and hook_block_view in Drupal 7).

Drupal module settings page construction

I am refactoring some code is a Drupal module I wrote sometime age. In order for others to use it, I am adding a configuration page.
I have successfully defined a fieldset but I don't know how to 'insert' content in to it.
The following code sets up radios for each node type defined on my site:
$node_types = node_get_types('names');
$test = array(
'#title' => t('tweeting node'),
'#type' => 'radios',
'#options' => $node_types,
'#default_value' => 'Page',
'#weight' => 0,
);
And the following defines my fieldset into which I want to insert the radio buttons generated above:
$form['twitterhelper_nodecollection'] = array(
'#type' => 'fieldset',
'#title' => t('select a node'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#parents' => $test,
);
to add any form element inside the fieldset you should insert this form element inside the field set array ...
E.g
$form['myfieldset'] = array(
'#type' => 'fieldset' ,
'#collapsible' => TRUE ,
'#title' => t('My FIeldset'),
'#attributes' => array('id' => 'myfieldset-id'),
);
$form['myfieldset']['myradios'] = array(
'#type' => 'radios' ,
'#attributes' => array('id' =>'myradio-attributes') ,
....etc
);
so the fieldset is the parent of the radios not the contrast
hop that help you
UPDATE:
you can append the radios inside the field set by using the jquery as the following
jQuery(document).ready(start) ;
function start(){
jQuery("#myradio-attributes").appendTo("#myfieldset-id");
// i added this id by '#attributes'
}
but its not the drupal way

Drupal - add form element on successful submission

In a Drupal custom module, I want to make $form['link_wrapper'] conditional on a successful submission of the form but this is not a very successful way of doing this. Can anyone suggest better approach.
function my_function_my_form($form_state){
//echo "-" . $form_state['post']['op'] ."-";
//die();
global $base_root;
$form = array();
$form ['query_type'] =array (
'#type' => 'radios',
'#title' => t('Select from available Queries'),
'#options' => array(
"e_commerce_orders" => t("Query1"),
"new_orders" => t("Query2"),
"cancelled_orders" => t("Query3")),
'#required' => TRUE,
);
// only show link when submitted
if($form_state['post']['op'] == 'Submit')
{
$form['link_wrapper'] = array(
'#prefix' => '<div>',
'#value' => l("Click to View file"),
'#suffix' => '</div><br><br>',
);
}
// add submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
return $form;
}
Have you tried setting your condition in the validate hook?
Something like:
function my_function_my_form_validate($form_state){
//some condition is true
$form_state['something'] = TRUE;
}
http://api.drupal.org/api/function/hook_validate/6
This is rough. I can't remember the args for hook_validate

AJAX in Drupal Forms?

How would you go about constructing a step by step form that uses AJAX through Drupal to pull the next form step?
For example,
Step 1:
I like Baseball
I don't like Baseball.
When that person clicks on either Like or Don't Like, I want to use AJAX to recognize and pull the next part of the form, remove/hide the first section since its not needed, and present the next section.
Example:
Step 1:
I like Baseball
*click
(fade out)
Step 2:
My favorite team is __________
The player I like most is __________
What is the best way to do this through Drupal Form API? I know how to build the forms and modules, but I have never used AJAX yet. I know a few things exist out there that are supposed to help, but I wanted to know if anyone here has done it and how they approached it.
You may want to give a look at the AHAH helper module.
usually i am create full form with fieldsets, then control them manually by jquery.
i assume there lot of ready to go modules in drupal, some of these:
http://drupal.org/project/conditional_fields / http://drupal.org/project/multistep
also: http://www.google.ru/search?q=drupal+multistep+ajax+form
If you don't want to write any code, and don't need the entered data to be drupal nodes, I suggest using the webform module. It has a pretty simple UI for building forms, and allows you do do multipage forms with conditional fields. You can then export the results as CSV, email them, etc.
I have made to solutions for this problem in drupal 7. First one I solve it with Ajax as was requested(if someone want I can convert this to drupal6), however it should be better to solve this using attribute #states. So also made a solution in the bottom using states.
How to solve this using Ajax:
function ajax_in_drupal_form($form, &$form_state)
{
$baseball = array(
'like' => t('I like Baseball'),
'unlike' => t('I don\'t like Baseball')
);
$form['step'] = array(
'#prefix' => '<div id="baseball-wrapper">',
'#suffix' => '</div>',
);
if ($form_state['values']['baseball'] == 'like') {
$form['step']['team'] = array(
'#type' => 'textfield',
'#title' => t('My favorite team is'),
);
$form['step']['player'] = array(
'#type' => 'textfield',
'#title' => t('The player I like most is'),
);
}
else if ($form_state['values']['baseball'] == 'unlike') {
$form['step']['other'] = array(
'#type' => 'textfield',
'#title' => t('What do you like'),
);
}
else {
$form['step']['baseball'] = array(
'#type' => 'radios',
'#options' => $baseball,
'#title' => t('Select your option'),
'#ajax' => array(
'callback' => 'ajax_update_step_callback',
'wrapper' => 'baseball-wrapper',
),
);
}
return $form;
}
function ajax_update_step_callback($form, $form_state) {
return $form['step'];
}
Here is the solution using #states(The preferred way of solving it):
function states_in_drupal_form($form, &$form_state)
{
$baseball = array(
'like' => t('I like Baseball'),
'unlike' => t('I don\'t like Baseball')
);
// step 1
$form['step']['baseball'] = array(
'#type' => 'radios',
'#options' => $baseball,
'#title' => t('Select your option'),
'#states' => array(
'invisible' => array(':input[name="baseball"]' => array('checked' => TRUE),
),
)
);
// step 2 like baseball
$form['step']['team'] = array(
'#type' => 'textfield',
'#title' => t('My favorite team is'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'like')),
)
);
$form['step']['player'] = array(
'#type' => 'textfield',
'#title' => t('The player I like most is'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'like')),
)
);
// step 2 I don't like baseball
$form['step']['other'] = array(
'#type' => 'textfield',
'#title' => t('What do you like'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'unlike')),
)
);
return $form;
}

Resources