views:

214

answers:

1

Hello Everyone,

I have created my own little form element for inputting time's. Since I need this on a few places in my application, i decided to create a seperate element for it.

Here is the code so fa:

class EventManager_Form_Element_Time extends Zend_Form_Element 
{
    public function init()
    {
        parent::init();
        $this->addDecorator('ViewScript', array(
            'viewScript' =>  'time.phtml'
        ));

        $this->addValidator(new Zend_Validate_Regex('/^[0-9]+:[0-9]+:[0-9]+$/'));
    }

    public function setValue($value)
    {
        if(is_array($value))
        {
            @list($hours, $minutes, $seconds) = $value;         
            $value = sprintf('%s:%s:%s', $hours, $minutes, $seconds);
        }

        return parent::setValue($value);
    }
}

The corresponding view script, which I have created is:

    <?php @list($hours, $minutes, $seconds) = explode(':', $this->element->getValue()); ?>
<dt>
    <?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
</dt>
<dd>
    <select id="<?= $this->element->getName();?>">
        <option value="00">00</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
        <option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
    </select>
    <select id="<?= $this->element->getName();?>">
        <option value="00">00</option>
        <option value="15">15</option>
        <option value="30">30</option>
        <option value="45">45</option>
    </select>

    <input id="<?= $this->element->getName(); ?>" 
           type="hidden"
           name="<?= $this->element->getName(); ?>[]" 
           value="00" />



    <?php if(count($this->element->getMessages()) > 0): ?>
        <?= $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
</dd>

My problem is that sometimes i want to set a default selected value to my selecboxes when I populate my form. The question is how?

Is there anyone that could help me out with this?

Thanks.

+1  A: 

You could do something like this (untested):

Controller:

$form = new Your_Form();
$form->yourDateElement->setValue( '12:30:00' );
$this->view->form = $form;

View:

<?= $this->form ?>

Form element viewscript:

    <?php @list($hours, $minutes, $seconds) = explode(':', $this->element->getValue()); ?>
<dt>
    <?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
</dt>
<dd>
    <select id="<?= $this->element->getName();?>" name="<?= $this->element->getName(); ?>[]">
    <?
        for( $h = 0; $h < 24; $h++ ):
            $selected = $h == $hours ? ' selected="selected"' : '';
            $paddedHours = sprintf( '%02d', $h );
    ?>
        <option value="<?= $paddedHours ?>"<?= $selected ?>><?= $paddedHours ?></option>
    <? endfor; ?>
    </select>
    <select id="<?= $this->element->getName();?>" name="<?= $this->element->getName(); ?>[]">
    <?
        for( $m = 0; $m < 60; $m += 15 ):
            $selected = $m == $minutes ? ' selected="selected"' : '';
            $paddedMinutes = sprintf( '%02d', $m );
    ?>
        <option value="<?= $paddedMinutes ?>"<?= $selected ?>><?= $paddedMinutes ?></option>
    <? endfor; ?>
    </select>

    <input id="<?= $this->element->getName(); ?>" 
           type="hidden"
           name="<?= $this->element->getName(); ?>[]" 
           value="00" />



    <?php if(count($this->element->getMessages()) > 0): ?>
        <?= $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
</dd>

BTW:
I don't think it's all gonna work so easily. You see, your viewscript has 3 elements: 2 selects and 1 hidden input, but your form element doesn't account for these.

Have a look at Matthew Weier O'Phinney's blog post on how to create composite form elements. That should give you some ideas.

fireeyedboy
@fireeyedboy saw the tutorial but i down't know how to change the input boxes to select boxes.
sanders