This is kind of an old topic.
But I came across it searching for the same thing. And it was the only topic I found related to this.
Anyway, since I dind't find the solution in any other topic. I solved it myself.
And I think it's good to share it with you all.
What I did, was I modified the /ow_core/form_element.php class a bit.
Search for the Selectbox class, and add a private variable:
/**
* Form element: Selectbox.
*
* @author Sardar Madumarov
* @package ow_core
* @since 1.0
*/
class Selectbox extends InvitationFormElement
{
/**
* Input options.
*
* @var array
*/
private $options = array();
private $selected = null;
Also make a public function that sets the selected value.
I added mine after the setOptions function:
/**
* Sets field options.
*
* @param array $options
* @return Selectbox
* @throws InvalidArgumentException
*/
public function setOptions( $options )
{
if ( $options === null || !is_array($options) )
{
throw new InvalidArgumentException('Array is expected!');
}
$this->options = $options;
return $this;
}
public function setSelected($key) {
$this->selected = $key;
return $this;
}
And last, modfy the render function:
/**
* @see FormElement::renderInput()
*
* @param array $params
* @return string
*/
public function renderInput( $params = null ) {
parent::renderInput($params);
$optionsString = '';
if ( $this->hasInvitation )
{
$optionsString .= UTIL_HtmlTag::generateTag('option', array('value' => ''), true, $this->invitation);
}
foreach ( $this->options as $key => $value )
{
$attrs = array();
if($this->selected == $key) { $attrs['selected'] = 'selected'; }
$attrs['value'] = $key;
$optionsString .= UTIL_HtmlTag::generateTag('option', $attrs, true, trim($value));
}
return UTIL_HtmlTag::generateTag('select', $this->attributes, true, $optionsString);
}
Now you can call the function from within your code:
$fieldDay = new Selectbox('day');for($i=1;$i<10;$i++) { $fieldDay->addOption($i, "0".$i); }
for($i=10;$i<32;$i++) { $fieldDay->addOption($i, $i); }
$fieldDay->setLabel(OW::getLanguage()->text('myplugin', 'planform_label_date'));
$fieldDay->setRequired();
$fieldDay->setHasInvitation(false);
$fieldDay->setSelected('9');
$form->addElement($fieldDay);