You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alfredo Manuel Osorio Martinez <al...@afirme.com> on 2013/05/23 00:11:45 UTC

Custom error message per converter

Hello,

Is it possible to configure a custom error message per converter?

For example I have this in my xwork-conversion.properties:
java.util.Date=mx.com.afirme.midas2.converter.DateConverter

Whenever a Date conversion fails in any action I'd like to show a message like this:
Incorrect format, expected mm/dd/yyyy

I don't want to define a custom message per property as mentioned in the documentation:
http://struts.apache.org/release/2.3.x/docs/type-conversion.html

"However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name."


Alfredo Osorio


Re: Custom error message per converter

Posted by Lukasz Lenart <lu...@apache.org>.
It isn't possible right now, only invalid.fieldvalue.<propertyName> -
you can always request the change via
https://issues.apache.org/jira/browse/WW


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/


2013/5/23 Alfredo Manuel Osorio Martinez <al...@afirme.com>:
> Conversion errors are always reported with a default message defined in the xwork.default.invalid.fieldvalue (Invalid field value for field "xxx", where xxx is the field name) but what I want is a way to be more specific depending of the type. For example for java.math.BigDecimal a custom message for java.lang.Integer another message and so on and if no custom message is found the default is showed.
>
> -----Mensaje original-----
> De: Alfredo Manuel Osorio Martinez
> Enviado el: Thursday, May 23, 2013 8:50 AM
> Para: Struts Users Mailing List
> Asunto: RE: Custom error message per converter
>
> Sure.
>
> public class DateConverter extends StrutsTypeConverter {
>     @SuppressWarnings("rawtypes")
>         public Object convertFromString(Map context, String[] values, Class toClass) {
>         if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) {
>             SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
>             try {
>                 return sdf.parse(values[0]);
>             }
>             catch(ParseException e) {
>                 throw new TypeConversionException(e);
>             }
>         }
>         return null;
>     }
>     @SuppressWarnings("rawtypes")
>         public String convertToString(Map context, Object o) {
>         if (o instanceof Date) {
>             SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
>             return sdf.format((Date)o);
>         }
>         return "";
>     }
> }
>
>
> -----Mensaje original-----
> De: Lukasz Lenart [mailto:lukaszlenart@apache.org] Enviado el: Thursday, May 23, 2013 8:01 AM
> Para: Struts Users Mailing List
> Asunto: Re: Custom error message per converter
>
> Could you show source code of your DateConverter?
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
>
> 2013/5/23 Alfredo Manuel Osorio Martinez <al...@afirme.com>:
>> Hello,
>>
>> Is it possible to configure a custom error message per converter?
>>
>> For example I have this in my xwork-conversion.properties:
>> java.util.Date=mx.com.afirme.midas2.converter.DateConverter
>>
>> Whenever a Date conversion fails in any action I'd like to show a message like this:
>> Incorrect format, expected mm/dd/yyyy
>>
>> I don't want to define a custom message per property as mentioned in the documentation:
>> http://struts.apache.org/release/2.3.x/docs/type-conversion.html
>>
>> "However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name."
>>
>>
>> Alfredo Osorio
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Custom error message per converter

Posted by Alfredo Manuel Osorio Martinez <al...@afirme.com>.
Conversion errors are always reported with a default message defined in the xwork.default.invalid.fieldvalue (Invalid field value for field "xxx", where xxx is the field name) but what I want is a way to be more specific depending of the type. For example for java.math.BigDecimal a custom message for java.lang.Integer another message and so on and if no custom message is found the default is showed. 

-----Mensaje original-----
De: Alfredo Manuel Osorio Martinez 
Enviado el: Thursday, May 23, 2013 8:50 AM
Para: Struts Users Mailing List
Asunto: RE: Custom error message per converter

Sure.

public class DateConverter extends StrutsTypeConverter {
    @SuppressWarnings("rawtypes")
	public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            try {
                return sdf.parse(values[0]);
            }
            catch(ParseException e) {
                throw new TypeConversionException(e);
            }
        }
        return null;
    }
    @SuppressWarnings("rawtypes")
	public String convertToString(Map context, Object o) {
        if (o instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            return sdf.format((Date)o);
        }
        return "";
    }
}


-----Mensaje original-----
De: Lukasz Lenart [mailto:lukaszlenart@apache.org] Enviado el: Thursday, May 23, 2013 8:01 AM
Para: Struts Users Mailing List
Asunto: Re: Custom error message per converter

Could you show source code of your DateConverter?


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/


2013/5/23 Alfredo Manuel Osorio Martinez <al...@afirme.com>:
> Hello,
>
> Is it possible to configure a custom error message per converter?
>
> For example I have this in my xwork-conversion.properties:
> java.util.Date=mx.com.afirme.midas2.converter.DateConverter
>
> Whenever a Date conversion fails in any action I'd like to show a message like this:
> Incorrect format, expected mm/dd/yyyy
>
> I don't want to define a custom message per property as mentioned in the documentation:
> http://struts.apache.org/release/2.3.x/docs/type-conversion.html
>
> "However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name."
>
>
> Alfredo Osorio
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Custom error message per converter

Posted by Alfredo Manuel Osorio Martinez <al...@afirme.com>.
Sure.

public class DateConverter extends StrutsTypeConverter {
    @SuppressWarnings("rawtypes")
	public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            try {
                return sdf.parse(values[0]);
            }
            catch(ParseException e) {
                throw new TypeConversionException(e);
            }
        }
        return null;
    }
    @SuppressWarnings("rawtypes")
	public String convertToString(Map context, Object o) {
        if (o instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            return sdf.format((Date)o);
        }
        return "";
    }
}


-----Mensaje original-----
De: Lukasz Lenart [mailto:lukaszlenart@apache.org] 
Enviado el: Thursday, May 23, 2013 8:01 AM
Para: Struts Users Mailing List
Asunto: Re: Custom error message per converter

Could you show source code of your DateConverter?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/


2013/5/23 Alfredo Manuel Osorio Martinez <al...@afirme.com>:
> Hello,
>
> Is it possible to configure a custom error message per converter?
>
> For example I have this in my xwork-conversion.properties:
> java.util.Date=mx.com.afirme.midas2.converter.DateConverter
>
> Whenever a Date conversion fails in any action I'd like to show a message like this:
> Incorrect format, expected mm/dd/yyyy
>
> I don't want to define a custom message per property as mentioned in the documentation:
> http://struts.apache.org/release/2.3.x/docs/type-conversion.html
>
> "However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name."
>
>
> Alfredo Osorio
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Custom error message per converter

Posted by Lukasz Lenart <lu...@apache.org>.
Could you show source code of your DateConverter?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/


2013/5/23 Alfredo Manuel Osorio Martinez <al...@afirme.com>:
> Hello,
>
> Is it possible to configure a custom error message per converter?
>
> For example I have this in my xwork-conversion.properties:
> java.util.Date=mx.com.afirme.midas2.converter.DateConverter
>
> Whenever a Date conversion fails in any action I'd like to show a message like this:
> Incorrect format, expected mm/dd/yyyy
>
> I don't want to define a custom message per property as mentioned in the documentation:
> http://struts.apache.org/release/2.3.x/docs/type-conversion.html
>
> "However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name."
>
>
> Alfredo Osorio
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org