I'm creating a module with a form using the API form. The form have two fields: the first a date_popup type, second a select type. I need that when the users pick on sundays in the calendar popup the second field show only one option instead of two like the other days of the week. The form code:

function reservas_seleccion_form($form, &$form_state)
{
	$form['Fecha']=array(
       	'#title' => t('Fecha'),
		'#type' => 'date_popup', //Provided by the date_popup module
		'#date_format' => 'j F Y', 
		'#date_year_range'=> '0:+1', //Limit the year range to the next year
		'#datepicker_options' => array('minDate' => 0,'firstDay' => 1,),  //(minDate)Prevent past dates, (firstDay) set monday first day 
		'#date_label_position' => 'none', //Delete title "Date"
		'#default_value' => date('Y-m-d'),	
	);

	$form['Regimen']=array(
		'#title' => t('Régimen'),
		'#type' => 'select', 
		'#options' => array( 
			1 => 'Comida',
			2 => 'Cena',),
		'#after_build' => array('reservas_eventbook_get_rating_widget_after_build'), // to remove "-seleccionar-" in select combo	
       );
}

Any idea? Thank in advance!

Comments

jmomandown’s picture

You really have two options here: The first would be to have multiple List fields with the desired values and to hide/show them through the #states property. If the form is large and you are concerned about performance or these fields are likely to change often in the future, this is not a good approach.

The second option is to limit the values based on a tree hierarchy where the deeper you go in the tree, the more options are eliminated and not displayed. This is a better approach for performance but much more complex. For a great example, take a look at the Hierarchical Select Model and its code: http://drupal.org/project/hierarchical_select

i32orgrr’s picture

Thank you for your answer. I know the two options that you tell me. I thought of doing it in the first option. My problem is that i don't know what the value in de #state for Mondays for date_popup:

{	
	$form['Fecha']=array(
       	'#title' => t('Fecha'),
		'#type' => 'date_popup', 
		'#date_format' => 'j F Y', 
		'#date_year_range'=> '0:+1', 
		'#datepicker_options' => array('minDate' => 0,'firstDay' => 1,), 
		'#date_label_position' => 'none', //Delete title "Date"
		'#default_value' => date('Y-m-d'),
	);


	$form['Regimen1']=array(
		'#title' => t('Régimen'),
		'#type' => 'select', 
		'#options' => array( 
			1 => 'Comida'),
		'#after_build' => array('reservas_eventbook_get_rating_widget_after_build'), 
		'#states' => array( 'visible' => array(':input[name="Fecha"]' => array('value' => '')),),
	
	$form['Regimen2']=array(
		'#title' => t('Régimen'),
		'#type' => 'select', 
		'#options' => array( 
			1 => 'Comida',
			2 => 'Cena'),
		'#after_build' => array('reservas_eventbook_get_rating_widget_after_build'), 
		'#states' => array( 'visible' => array(':input[name="Fecha"]' => array('value' => '')),),
			
       );

I do this but i don't know the 'value' for mondays and in the second field the 'value' to the others days of the week. Any idea?

jmomandown’s picture

@i32orgrr: Sure. So if you chose to go the route of the first option the date module will still store the value as a date irrespective of the day. So you need to run a little php to check and see what day the date is on like the following:

<?php
/* This should be changed to grab the value from the inputted date, I suggest in the hook validation function of your form, but I placed it here to show how things flow */
$date = $date_field['und'][0]['value'];

/* Lets use the php date function to get the day of the week, where 0 is Sunday and 6 is Saturday! */
$day_of_week = date( "w", $date);

/* Now it is just a simple if statement */
if ($day_of_week == '0') {
  // Trigger your state change
}
?>

Sorry for the rushed response, but I hope that gets you off in the right direction

i32orgrr’s picture

I try this but the fields don't make any things. I'm making a validation function:

function reservas_validate_form($form, &$form_state)
{
	$date =$date_field['und'][0]['value'];
	$day_of_week = date( "w", $date);
	if ($day_of_week == '0')
		$form_state['values']= 0;
	else
		$form_state['values']= 1;
       		
}

and the form:

function reservas_seleccion_form($form, &$form_state)
{
	
	$form['Fecha']=array(
       	'#title' => t('Fecha'),
		'#type' => 'date_popup', //Provided by the date_popup module
		'#date_format' => 'j F Y', 
		'#date_year_range'=> '0:+1', //Limit the year range to the next year
		'#datepicker_options' => array('minDate' => 0,'firstDay' => 1,),  //(minDate)Prevent past dates, (firstDay) set monday first day 
		'#date_label_position' => 'none', //Delete title "Date"
		'#default_value' => date('Y-m-d'),
		//'#required' => TRUE,		
	);
	
	$form['Regimen1']=array(
		'#title' => t('R&eacute;gimen'),
		'#type' => 'select', 
		'#options' => array( 
			1 => 'Comida'),
		'#after_build' => array('reservas_eventbook_get_rating_widget_after_build'), 
		'#states' => array( 'visible' => array(':input[name="Fecha"]' => array('value' => '0')),),
	);

	$form['Regimen2']=array(
		'#title' => t('R&eacute;gimen'),
		'#type' => 'select', 
		'#options' => array( 
			1 => 'Comida',
			2 => 'Cena'),
		'#after_build' => array('reservas_eventbook_get_rating_widget_after_build'), 
		'#states' => array( 'visible' => array(':input[name="Fecha"]' => array('value' => '1')),),

       );
	
 return $form;
}

any idea? thank you very much for your help!

damienmckenna’s picture

Version: 7.x-2.6 » 7.x-2.x-dev
Issue summary: View changes
damienmckenna’s picture

Assigned: i32orgrr » Unassigned