One of the pieces I need to build for the app I'm working on is a scheduling form which shows days of the week monday through sunday and two dropdowns for each day, one for opening time and one for closing time. Eventually I'm going to make this more sophisticated - maybe integrate it with a visual calendar, enable time slots, stuff like that. But for now I just want a nice clean way to encapsulate this type of input and share it among multiple forms.
I started by making a custom select element with all the hours listed, in half-hour intervals, like so:
[PHP]
<?php
class My_Form_Element_HalfHours extends Zend_Form_Element_Select
{
public function init()
{
parent::init();
$vals = array(
'0000' => '12:00 AM',
'0030' => '12:30 AM',
... truncated ...
'2330' => '11:30 PM',
);
$this->setMultiOptions($vals);
}
}
[/PHP]
This way I can share this dropdown anywhere I need it, etc. This works fine. If I wanted to I could implement this in a specific form a bunch of times, get and retrieve the values as I would any other multiselect. But, I don't want to do that because I know I'm going to need to reuse this same type of input among multiple forms and I would like to encapsulate it so I only have to maintain it in one place.
I think what I want to do is make a composite element sort of like Matthew talks about here: Creating composite elements - phly, boy, phly . But I don't think it's just as easy as making a decorator which calls $view->formSelect() to render the selects, because I want to use the HalfHour element I already made.
I tried adding them into a SubForm like:
[PHP]
class My_Form_SubForm_WeeklySchedule extends Zend_Form_SubForm
{
public function init()
{
$mon1 = new My_Form_Element_HalfHours('monopen');
$this->addElement($mon1);
$mon2 = new My_Form_Element_HalfHours('monclose');
$this->addElement($mon2);
$tues1 = new My_Form_Element_HalfHours('tuesopen');
$this->addElement($tues1);
$tues2 = new My_Form_Element_HalfHours('tuesclose');
$this->addElement($tues2);
... etc ...
$this->addDisplayGroup(array('monopen','monclose','tueso pen','tuesclose', .....), 'scheduleGroup', array(
'legend' => 'my schedule'
));
}
}
[/PHP]
then in my main form I add it like:
[PHP]
$this->addSubForm(new My_Form_Element_WeeklySchedule('businessHours'), 'element');
[/PHP]
This seems like it should work but none of the selects show up on my form at all. I also made a custom decorator for it just for testing -
[PHP]
class My_Form_Decorator_WeeklySchedule extends Zend_Form_Decorator_Abstract
{
public function render($content)
{
echo "weekly schedule decorator<br/>";
return $content;
}
}[/PHP]
I know that the subform code is running because I see the echo coming out on my form. However I'm not seeing any selects.
So, I'm not sure if I am approaching this correctly. Does it make sense to use a subform here as opposed to some type of composite element that extends Zend_Form_Element_Xhtml? If I do it that way how can I add my custom HalfHour selects instead of just running $view->formSelect() and having to create each one by hand? Should I be using something else like maybe a ViewScript decorator here to render everything out the way I want it? Pointers / suggestions would be appreciated.
Last edited by Rhino; 04-14-2009 at 01:16 AM.