21 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
44 var $_options = array();
52 var $_multiple =
false;
68 var $_value = array();
79 function XoopsFormSelect($caption, $name, $value = null, $size = 1, $multiple =
false)
81 $this->setCaption($caption);
82 $this->setName($name);
83 $this->_multiple = $multiple;
84 $this->_size = intval($size);
86 $this->setValue($value);
97 return $this->_multiple;
116 function getValue($encode =
false)
119 return $this->_value;
122 foreach($this->_value as $val) {
123 $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
133 function setValue($value)
135 if (is_array($value)) {
136 foreach($value as $v) {
137 $this->_value[] = $v;
139 } elseif (isset($value)) {
140 $this->_value[] = $value;
150 function addOption($value, $name =
'')
153 $this->_options[$value] = $name;
155 $this->_options[$value] = $value;
168 $this->addOption($k, $v);
181 function getOptions($encode =
false)
184 return $this->_options;
187 foreach($this->_options as $val => $name) {
188 $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
200 $ele_name = $this->getName();
201 $ele_title = $this->getTitle();
202 $ele_value = $this->getValue();
203 $ele_options = $this->getOptions();
204 $ret =
'<select size="' . $this->getSize() .
'"' . $this->getExtra();
205 if ($this->isMultiple() !=
false) {
206 $ret .=
' name="' . $ele_name .
'[]" id="' . $ele_name .
'" title="'. $ele_title.
'" multiple="multiple">' ;
208 $ret .=
' name="' . $ele_name .
'" id="' . $ele_name .
'" title="'. $ele_title.
'">' ;
210 foreach($ele_options as $value => $name) {
211 $ret .=
'<option value="' . htmlspecialchars($value, ENT_QUOTES) .
'"';
212 if (count($ele_value) > 0 && in_array($value, $ele_value)) {
213 $ret .=
' selected="selected"';
215 $ret .=
'>' . $name .
'</option>' ;
226 function renderValidationJS()
229 if (! empty($this->customValidationCode)) {
230 return implode(
"\n", $this->customValidationCode);
232 } elseif ($this->isRequired()) {
233 $eltname = $this->getName();
234 $eltcaption = $this->getCaption();
236 $eltmsg = str_replace(
'"',
'\"', stripslashes($eltmsg));
237 return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" .
"for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" .
"if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";