Code Duplication    Length = 21-23 lines in 2 locations

htdocs/xoops_lib/Xoops/Form/Checkbox.php 1 location

@@ 113-135 (lines=23) @@
110
     *
111
     * @return string
112
     */
113
    public function renderValidationJS()
114
    {
115
        // render custom validation code if any
116
        if (!empty($this->customValidationCode)) {
117
            return implode("\n", $this->customValidationCode);
118
            // generate validation code if required
119
        } elseif ($this->isRequired()) {
120
            $eltname = $this->getName();
121
            $eltcaption = $this->getCaption();
122
            $eltmsg = empty($eltcaption)
123
                ? sprintf(\XoopsLocale::F_ENTER, $eltname)
124
                : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
125
            $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
126
            return "\n"
127
            . "var hasChecked = false; var checkBox = myform.elements['{$eltname}'];"
128
            . " if (checkBox.length) {for (var i = 0; i < checkBox.length; i++)"
129
            . " {if (checkBox[i].checked == true) {hasChecked = true; break;}}}"
130
            . "else{if (checkBox.checked == true) {hasChecked = true;}}"
131
            . "if (!hasChecked) {window.alert(\"{$eltmsg}\");if (checkBox.length)"
132
            . " {checkBox[0].focus();}else{checkBox.focus();}return false;}";
133
        }
134
        return '';
135
    }
136
}
137

htdocs/xoops_lib/Xoops/Form/Select.php 1 location

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