| @@ 150-170 (lines=21) @@ | ||
| 147 | * |
|
| 148 | * @return string |
|
| 149 | */ |
|
| 150 | public function renderValidationJS() |
|
| 151 | { |
|
| 152 | // render custom validation code if any |
|
| 153 | if (!empty($this->customValidationCode)) { |
|
| 154 | return implode("\n", $this->customValidationCode); |
|
| 155 | // generate validation code if required |
|
| 156 | } elseif ($this->isRequired()) { |
|
| 157 | $eltname = $this->getName(); |
|
| 158 | $eltcaption = $this->getCaption(); |
|
| 159 | $eltmsg = empty($eltcaption) |
|
| 160 | ? sprintf(\XoopsLocale::F_ENTER, $eltname) |
|
| 161 | : sprintf(\XoopsLocale::F_ENTER, $eltcaption); |
|
| 162 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
|
| 163 | return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" |
|
| 164 | . "for (i = 0; i < selectBox.options.length; i++ ) { " |
|
| 165 | . "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') " |
|
| 166 | . "{ hasSelected = true; break; } }" . "if (!hasSelected) " |
|
| 167 | . "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
|
| 168 | } |
|
| 169 | return ''; |
|
| 170 | } |
|
| 171 | } |
|
| 172 | ||
| @@ 126-148 (lines=23) @@ | ||
| 123 | * |
|
| 124 | * @return string |
|
| 125 | */ |
|
| 126 | public function renderValidationJS() |
|
| 127 | { |
|
| 128 | // render custom validation code if any |
|
| 129 | if (!empty($this->customValidationCode)) { |
|
| 130 | return implode("\n", $this->customValidationCode); |
|
| 131 | // generate validation code if required |
|
| 132 | } elseif ($this->isRequired()) { |
|
| 133 | $eltname = $this->getName(); |
|
| 134 | $eltcaption = $this->getCaption(); |
|
| 135 | $eltmsg = empty($eltcaption) |
|
| 136 | ? sprintf(\XoopsLocale::F_ENTER, $eltname) |
|
| 137 | : sprintf(\XoopsLocale::F_ENTER, $eltcaption); |
|
| 138 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
|
| 139 | return "\n" |
|
| 140 | . "var hasChecked = false; var checkBox = myform.elements['{$eltname}'];" |
|
| 141 | . " if (checkBox.length) {for (var i = 0; i < checkBox.length; i++)" |
|
| 142 | . " {if (checkBox[i].checked == true) {hasChecked = true; break;}}}" |
|
| 143 | . "else{if (checkBox.checked == true) {hasChecked = true;}}" |
|
| 144 | . "if (!hasChecked) {window.alert(\"{$eltmsg}\");if (checkBox.length)" |
|
| 145 | . " {checkBox[0].focus();}else{checkBox.focus();}return false;}"; |
|
| 146 | } |
|
| 147 | return ''; |
|
| 148 | } |
|
| 149 | } |
|
| 150 | ||