PDA

View Full Version : I18 and Validator



fmourioux
Aug 25th, 2004, 02:38 AM
Hi,

today, i try the spring validator and i have 2 questions :

- is it possible to show errors in a javascript popup ?
- in the errors.rejectValue, can i use an i18n value like this :

("field", "errors.minlength", new Object[]{"i18n field name","2"},"");

instead of :

("field", "errors.minlength", new Object[]{"field name","2"},"");

Thanks,

Fabien.

uze
Aug 25th, 2004, 01:47 PM
To show errors in a javascript alert, put the following at the end of your page:


<spring&#58;hasBindErrors name="reoDataModel">
<script language="javascript">
errors='You have <c&#58;out value="$&#123;errors.errorCount&#125;" /> errors&#58;\n';
errors+='<c&#58;forEach var="error" items="$&#123;errors.allErrors&#125;">\n';
errors+='-<c&#58;out value="$&#123;error.defaultMessage&#125;" /></c&#58;forEach>';
alert&#40;errors&#41;;
</script>
</spring&#58;hasBindErrors>

cheers,

Uze

fmourioux
Aug 26th, 2004, 04:50 AM
Hi uze,

Thanks for your reply, it works fine to show errors in a javascript popup.

Do you know how can i get a complet translated message (cause error.defaultMessage is not i18n) ?

I tried to replace "<c:out value="${error.defaultMessage}" />" with <fmt:message key="${error.code}"/> but it doesn't working with all kind of message ex : typeMismatch.java.util.Date, it only display "???typeMismatch???".

Thanks,

Fabien.

uze
Aug 26th, 2004, 05:31 PM
Fabien,

Tu y etait presque :wink: ! In fact, to do things the right way,you must use the <spring:message> tag instead of JSTL's. So try this in your JSP:


<spring&#58;message code="$&#123;error.code&#125;" />

...and add errors like this:


errors.rejectValue&#40;"title", "someRessourceBundleErrorCode","Title can not be empty"&#41;;



That should make it!

Uze

fmourioux
Aug 27th, 2004, 04:15 AM
Hi uze,

that's i wasn't so far, but without your help....

Your help solve my pb, thanks a lot :-)

Fabien.

Rexxe
Aug 29th, 2004, 04:03 PM
I was wondering if something like this was possible:

messages.properties:


error.required=&#123;0&#125; is required
label.loginName=Login Name

controller/validator:


errors.rejectValue&#40;"loginName","required", new Object&#91;&#93;&#123;"label.loginName"&#125;, "Some Default"&#41;;

jsp:

???

If I want it to say "Login Name is required" when there is an error, how would I do that on the JSP? The <spring:message> tag has no where to pass in arguments. Do I have to resolve the message before putting it into the errors object using
getMessageSourceAccessor&#40;&#41;.getMessage&#40;"error.required", new Object&#91;&#93;&#123;getMessageSourceAccessor&#40;&#41;.getMessage&#40;"label.loginName"&#41;&#125;&#41;;

Thanks for any help!