如何在不清除输入的输入的情况下显示错误验证

时间:2011-12-01 16:27:03

标签: grails groovy

我验证输入的网址值的代码如下:

try{
        urls.eachWithIndex { u, i ->
            String countryCode = u.key;
            String url = urls["${u.key}"]
            if(url != ""){
            Boolean valid = helperService.isURLValid(url)
                if(valid == false){
                   this.errors.reject("Incorrect URL is entered for "+countries.get(countryCode)+" - please ensure to use a correct URL for More Games.")
                   return [];
                   // assert false
                    // params.gamesUrls = url
                }
            }
        }
        }catch (Exception ex) {
                    logger.warn("Incorrect URL is entered", ex)
                    return [];
        }

当URL验证失败时,将清除输入的值。

如何显示错误但不清除输入的输入(尝试返回[],但没有用)

这是gsp代码......

<g:form action="wizard">
            <table>
                <g:each in="${countries}" status="i" var="country">
                    <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                        <td><img border="0" src="/wizard/image/flags/${country.key}" alt="" /> <label for="gamesUrls.${country.key}">${country.value}</label></td>
                        <td><g:textField name="gamesUrls.${country.key}" style="width:350px" value="${moreGamesUrls.get(country.key)}" onchange="\$(this).addClassName('changed');" maxlength="255"/></td>
                        <td><input type="button" value="Revert" onClick="revertTextValue('gamesUrls.${country.key}')" /></td>
                    </tr>
                </g:each>
            </table>
            <g:submitButton name="back" value="Back" /> <g:submitButton name="next" value="Confirm" />

1 个答案:

答案 0 :(得分:0)

我可以通过检查gsp来解决这个问题

<g:if test="${moreGamesUrls.get(country.key) == null || 'User Cannot Edit this URL'.equals(moreGamesUrls?.get(country.key))}">
                            <td><g:textField name="gamesUrls.${country.key}" style="width:350px" data-default-value="" value="" onchange="\$(this).addClassName('changed');" maxlength="255"/></td>
                        </g:if>
                        <g:else>
                            <td><g:textField name="gamesUrls.${country.key}" style="width: 350px;" data-default-value="${moreGamesUrls?.get(country?.key)}" value="${moreGamesUrls?.get(country?.key)}" onchange="\$(this).addClassName('changed');" maxlength="255"/></td>
                        </g:else>