2010-04-20 98 views
0

我已經創建了我自己的用於輸入時間的小表單元素。由於我在我的應用程序的幾個地方需要這個,所以我決定爲它創建一個單獨的元素。在自定義中設置選定的項目Zend_Form_Element

這裏是代碼,使發:

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); 
    } 
} 

相應的視圖腳本,我已經創建是:

<?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> 

我的問題是,有時我想設置一個默認選擇的值當我填寫我的表單時,我的選擇框。問題是如何?

有沒有人能幫我解決這個問題?

謝謝。

回答

1

你可以做這樣的事情(未經測試):

控制器:

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

查看:

<?= $this->form ?> 

表單元素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:
我不認爲這一切都會如此輕鬆地工作。你看,你的觀點有3個元素:2個選擇和1個隱藏輸入,但你的表單元素沒有考慮到這些。

看看如何創建複合表單元素Matthew Weier O'Phinney's blog post。這應該給你一些想法。

+0

@fireeyedboy看到了教程,但我不知道如何更改輸入框來選擇框。 – sanders 2010-04-25 16:48:22

相關問題