Active
Project:
Date
Version:
7.x-2.x-dev
Component:
Date Popup
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
21 Feb 2013 at 16:02 UTC
Updated:
26 Oct 2018 at 03:40 UTC
Jump to comment: Most recent
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
Comment #1
jmomandown commentedYou 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
Comment #2
i32orgrr commentedThank 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:
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?
Comment #3
jmomandown commented@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:
Sorry for the rushed response, but I hope that gets you off in the right direction
Comment #4
i32orgrr commentedI try this but the fields don't make any things. I'm making a validation function:
and the form:
any idea? thank you very much for your help!
Comment #5
damienmckennaComment #6
damienmckenna