Setting selected item in custom Zend_Form_Element
Posted
by sanders
on Stack Overflow
See other posts from Stack Overflow
or by sanders
Published on 2010-04-20T18:23:36Z
Indexed on
2010/04/21
6:33 UTC
Read the original article
Hit count: 269
zend-framework
|zend-form-element
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.
© Stack Overflow or respective owner