You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jamie Orchard-Hays <ja...@dang.com> on 2004/06/28 23:12:27 UTC
Currency or Number Validators
Anyone out there already have a Number or Currency Validator that handles
commas they'd like to share?
Thanks,
Jamie
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Currency or Number Validators
Posted by Paul Ferraro <pm...@columbia.edu>.
Here's a rough draft for one. It leverages the java.text.DecimalFormat
class to handle any type of decorated number.
Unfortunately, I never got around to implementing client-side validation
ability.
import java.text.DecimalFormat;
import java.text.ParseException;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.BaseValidator;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;
public class DecimalValidator extends BaseValidator
{
private String invalidNumberFormatMessage;
private DecimalFormat decimalFormat = new DecimalFormat();
/**
* @see
org.apache.tapestry.valid.IValidator#toObject(org.apache.tapestry.form.IFormComponent,
java.lang.String)
*/
public Object toObject(IFormComponent field, String string) throws
ValidatorException
{
try
{
return this.decimalFormat.parse(string);
}
catch (ParseException e)
{
throw new
ValidatorException(this.buildInvalidNumericFormatMessage(field),
ValidationConstraint.NUMBER_FORMAT);
}
}
/**
* @see
org.apache.tapestry.valid.IValidator#toString(org.apache.tapestry.form.IFormComponent,
java.lang.Object)
*/
public String toString(IFormComponent field, Object object)
{
if (object == null) return null;
Number number = (Number) object;
return this.decimalFormat.format(number.doubleValue());
}
public String getInvalidNumberFormatMessage()
{
return this.invalidNumberFormatMessage;
}
public void setInvalidNumberFormatMessage(String
invalidNumberFormatMessage)
{
this.invalidNumberFormatMessage = invalidNumberFormatMessage;
}
public String getFormat()
{
return this.decimalFormat.toPattern();
}
public void setFormat(String pattern)
{
this.decimalFormat.applyPattern(pattern);
}
protected String buildInvalidNumericFormatMessage(IFormComponent field)
{
String pattern =
super.getPattern(this.getInvalidNumberFormatMessage(),
"invalid-numeric-format", field.getPage().getLocale());
return super.formatString(pattern, field.getDisplayName());
}
}
Jamie Orchard-Hays wrote:
>Anyone out there already have a Number or Currency Validator that handles
>commas they'd like to share?
>
>Thanks,
>Jamie
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org