Re: Localized warning in Validator
Hello, Thank you for your feedback. I managed to localized my warning, here is how I did, maybe it can help someone with the same concern: public class ValidatorWithWarningOption implements IValidator { final private TextField field; final private boolean warning; public ValidatorWithWarningOption(TextField field, boolean warning) { this.field = field; this.warning = warning; } @Override public void validate(IValidatable validatable) { String error = null; if(/*condition for error*/) { error = "ERROR"; } if( errors != null ){ if( this.warning ){ field.warn( formatError(error) ); }else{ field.error( formatError(error) ); } } } private String formatError(String errorKey) { String error = field.getString( errorKey ); error = error.replace( "${label}", field.getLabel().getObject() ); return error; } } In ValidatorWithWarningOption.properties file: ERROR = The field ${label} is not well formatted. On Wed, 2017-07-05 at 16:04 +0700, Maxim Solodovnik wrote: > +1 to unify this > I would expect similar behavior from all 3 (error, warn, info) > Never used warn BTW > > On Wed, Jul 5, 2017 at 3:56 PM, Martijn Dashorst >wrote: > > Hi Virginie, > > > > Your code doesn't look wrong. > > > > Here's a snippet from one of our applications that validates a social > > security number. It's a bit less verbose than your example, but seems to > > work similarly. And you've already used ValidationError so you are aware of > > its existence. > > > > public class BsnValidator implements IValidator > > { > > private static final long serialVersionUID = 1L; > > > > @Override > > public void validate(IValidatable validatable) > > { > > if (!ElfProef.isGeldigSofiNummer(sofinummer)) > > validatable.error(new ValidationError(this)); > > } > > } > > > > As you can see, we just call validatable.error(new ValidationError(this)); > > This gives Wicket the opportunity to locate the error message in localized > > resource bundles. > > > > E.g. in our Application_nl.properties we have this line: > > > > BsnValidator=Veld '${label}' bevat geen geldig BSN > > > > As for your second point: it appears that that is a functional omission. I > > typically would expect this to work for info, warn and error messages in a > > similar vain. Probably we should fix that in a future release (but would > > have to consult with the other wicket devs) > > > > For what I'm worth, in our applications we haven't seen the need for > > warnings using localization and substitutions, so we never encountered this > > omission. > > > > Martijn > > > > > > > > > > > > On Wed, Jul 5, 2017 at 10:30 AM, Virginie Garcin < > > virginie.gar...@equitativa.com> wrote: > > > > > Hello, > > > > > > I have a field validator. I want to be able to choose if it will be an > > > error (blocking the action) or a warning (just display a non-blocking > > > warning). > > > > > > Here is the code I use, that is working except the error/warning is not > > > localized: > > > > > > > > > public class ValidatorWithWarningOption implements IValidator { > > > > > > final private TextField field; > > > final private boolean warning; > > > > > > public ValidatorWithWarningOption(TextField field, > > > boolean warning) { > > > this.field = field; > > > this.warning = warning; > > > } > > > > > > @Override > > > public void validate(IValidatable validatable) { > > > String error = null; > > > > > > if(/*condition for error*/) { > > > error = "The field " + > > > field.getLabel().getObject() + " is not well formatted."; > > > } > > > > > > if( errors != null ){ > > > if( this.warning ){ > > > field.warn( error ); > > > }else{ > > > field.error( error ); > > > } > > > } > > > } > > > } > > > > > > 1/ Am I using the framework properly ? > > > There might be a better way to do it. Idealy, if it'd exist, I would have > > > used a method such as validatable.warn(IValidationError error) (like > > > validatable.error(IValidationError error)), so I would not have to pass > > > the field itself to the class and I would get proper localized warning, as > > > I get for errors. > > > Is there a way to do it ? > > > > > > > > > 2/ If I set the error like this: > > > ValidationError error = new ValidationError(
Re: Localized warning in Validator
+1 to unify this I would expect similar behavior from all 3 (error, warn, info) Never used warn BTW On Wed, Jul 5, 2017 at 3:56 PM, Martijn Dashorstwrote: > Hi Virginie, > > Your code doesn't look wrong. > > Here's a snippet from one of our applications that validates a social > security number. It's a bit less verbose than your example, but seems to > work similarly. And you've already used ValidationError so you are aware of > its existence. > > public class BsnValidator implements IValidator > { > private static final long serialVersionUID = 1L; > > @Override > public void validate(IValidatable validatable) > { > if (!ElfProef.isGeldigSofiNummer(sofinummer)) > validatable.error(new ValidationError(this)); > } > } > > As you can see, we just call validatable.error(new ValidationError(this)); > This gives Wicket the opportunity to locate the error message in localized > resource bundles. > > E.g. in our Application_nl.properties we have this line: > > BsnValidator=Veld '${label}' bevat geen geldig BSN > > As for your second point: it appears that that is a functional omission. I > typically would expect this to work for info, warn and error messages in a > similar vain. Probably we should fix that in a future release (but would > have to consult with the other wicket devs) > > For what I'm worth, in our applications we haven't seen the need for > warnings using localization and substitutions, so we never encountered this > omission. > > Martijn > > > > > > On Wed, Jul 5, 2017 at 10:30 AM, Virginie Garcin < > virginie.gar...@equitativa.com> wrote: > >> Hello, >> >> I have a field validator. I want to be able to choose if it will be an >> error (blocking the action) or a warning (just display a non-blocking >> warning). >> >> Here is the code I use, that is working except the error/warning is not >> localized: >> >> >> public class ValidatorWithWarningOption implements IValidator { >> >> final private TextField field; >> final private boolean warning; >> >> public ValidatorWithWarningOption(TextField field, >> boolean warning) { >> this.field = field; >> this.warning = warning; >> } >> >> @Override >> public void validate(IValidatable validatable) { >> String error = null; >> >> if(/*condition for error*/) { >> error = "The field " + >> field.getLabel().getObject() + " is not well formatted."; >> } >> >> if( errors != null ){ >> if( this.warning ){ >> field.warn( error ); >> }else{ >> field.error( error ); >> } >> } >> } >> } >> >> 1/ Am I using the framework properly ? >> There might be a better way to do it. Idealy, if it'd exist, I would have >> used a method such as validatable.warn(IValidationError error) (like >> validatable.error(IValidationError error)), so I would not have to pass >> the field itself to the class and I would get proper localized warning, as >> I get for errors. >> Is there a way to do it ? >> >> >> 2/ If I set the error like this: >> ValidationError error = new ValidationError( this, "ERROR" ); >> (with "ValidatorWithWarningOption.ERROR = The field ${label} is not well >> formatted." in the properties files) >> => For field.error( error ); I get a proper localized message (This call >> the method error(IValidationError error) from FormComponent) >> => But for field.warn( error ); I don't. As FormComponent has no method >> warn, this calls Component.warn(final Serializable message) so there is no >> localization handeling at all. >> How can I get my warnings localized ? >> >> >> Thanks for any help ! >> >> - >> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org >> For additional commands, e-mail: users-h...@wicket.apache.org >> >> > > > -- > Become a Wicket expert, learn from the best: http://wicketinaction.com -- WBR Maxim aka solomax - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Localized warning in Validator
Hi Virginie, Your code doesn't look wrong. Here's a snippet from one of our applications that validates a social security number. It's a bit less verbose than your example, but seems to work similarly. And you've already used ValidationError so you are aware of its existence. public class BsnValidator implements IValidator { private static final long serialVersionUID = 1L; @Override public void validate(IValidatable validatable) { if (!ElfProef.isGeldigSofiNummer(sofinummer)) validatable.error(new ValidationError(this)); } } As you can see, we just call validatable.error(new ValidationError(this)); This gives Wicket the opportunity to locate the error message in localized resource bundles. E.g. in our Application_nl.properties we have this line: BsnValidator=Veld '${label}' bevat geen geldig BSN As for your second point: it appears that that is a functional omission. I typically would expect this to work for info, warn and error messages in a similar vain. Probably we should fix that in a future release (but would have to consult with the other wicket devs) For what I'm worth, in our applications we haven't seen the need for warnings using localization and substitutions, so we never encountered this omission. Martijn On Wed, Jul 5, 2017 at 10:30 AM, Virginie Garcin < virginie.gar...@equitativa.com> wrote: > Hello, > > I have a field validator. I want to be able to choose if it will be an > error (blocking the action) or a warning (just display a non-blocking > warning). > > Here is the code I use, that is working except the error/warning is not > localized: > > > public class ValidatorWithWarningOption implements IValidator { > > final private TextField field; > final private boolean warning; > > public ValidatorWithWarningOption(TextField field, > boolean warning) { > this.field = field; > this.warning = warning; > } > > @Override > public void validate(IValidatable validatable) { > String error = null; > > if(/*condition for error*/) { > error = "The field " + > field.getLabel().getObject() + " is not well formatted."; > } > > if( errors != null ){ > if( this.warning ){ > field.warn( error ); > }else{ > field.error( error ); > } > } > } > } > > 1/ Am I using the framework properly ? > There might be a better way to do it. Idealy, if it'd exist, I would have > used a method such as validatable.warn(IValidationError error) (like > validatable.error(IValidationError error)), so I would not have to pass > the field itself to the class and I would get proper localized warning, as > I get for errors. > Is there a way to do it ? > > > 2/ If I set the error like this: > ValidationError error = new ValidationError( this, "ERROR" ); > (with "ValidatorWithWarningOption.ERROR = The field ${label} is not well > formatted." in the properties files) > => For field.error( error ); I get a proper localized message (This call > the method error(IValidationError error) from FormComponent) > => But for field.warn( error ); I don't. As FormComponent has no method > warn, this calls Component.warn(final Serializable message) so there is no > localization handeling at all. > How can I get my warnings localized ? > > > Thanks for any help ! > > - > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org > For additional commands, e-mail: users-h...@wicket.apache.org > > -- Become a Wicket expert, learn from the best: http://wicketinaction.com
Localized warning in Validator
Hello, I have a field validator. I want to be able to choose if it will be an error (blocking the action) or a warning (just display a non-blocking warning). Here is the code I use, that is working except the error/warning is not localized: public class ValidatorWithWarningOption implements IValidator { final private TextField field; final private boolean warning; public ValidatorWithWarningOption(TextField field, boolean warning) { this.field = field; this.warning = warning; } @Override public void validate(IValidatable validatable) { String error = null; if(/*condition for error*/) { error = "The field " + field.getLabel().getObject() + " is not well formatted."; } if( errors != null ){ if( this.warning ){ field.warn( error ); }else{ field.error( error ); } } } } 1/ Am I using the framework properly ? There might be a better way to do it. Idealy, if it'd exist, I would have used a method such as validatable.warn(IValidationError error) (like validatable.error(IValidationError error)), so I would not have to pass the field itself to the class and I would get proper localized warning, as I get for errors. Is there a way to do it ? 2/ If I set the error like this: ValidationError error = new ValidationError( this, "ERROR" ); (with "ValidatorWithWarningOption.ERROR = The field ${label} is not well formatted." in the properties files) => For field.error( error ); I get a proper localized message (This call the method error(IValidationError error) from FormComponent) => But for field.warn( error ); I don't. As FormComponent has no method warn, this calls Component.warn(final Serializable message) so there is no localization handeling at all. How can I get my warnings localized ? Thanks for any help ! - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org