Thiago, I'd like to donate the following code to the community or anybody
else who may run into this problem. It would be nice if it was a part of
tapestry's core validation library enabling us to validate currency/amounts.
I didn't include the js only because we aren't using it at my organization. 

maximum-precision=You may provide at most %d numbers for %s.
maximum-scale=You may provide at most %d decimal digits for %s.

//MaxScale

public class MaxScale extends AbstractValidator<Integer, BigDecimal> {

    public MaxScale() {
        super(Integer.class, BigDecimal.class, "maximum-scale");
    }

    @Override
    public void validate(Field field, Integer constraintValue,
MessageFormatter formatter, BigDecimal value)
            throws ValidationException 
    {        
        if (value.scale() > constraintValue)
            throw new ValidationException(buildMessage(formatter, field,
constraintValue));
    }

    private String buildMessage(MessageFormatter formatter, Field field,
Integer constraintValue) {
        return formatter.format(constraintValue, field.getLabel());
    }
    
    @Override
    public void render(Field field, Integer constraintValue,
MessageFormatter formatter, MarkupWriter writer,
            FormSupport formSupport) {
        formSupport.addValidation(field, "maxscale", buildMessage(formatter,
field, constraintValue), constraintValue);
    }

}

//MaxPrecision

public class MaxPrecision extends AbstractValidator<Integer, BigDecimal> {

    public MaxPrecision() {
        super(Integer.class, BigDecimal.class, "maximum-precision");
    }

    @Override
    public void validate(Field field, Integer constraintValue,
MessageFormatter formatter, BigDecimal value)
            throws ValidationException 
    {        
        if (value.precision() > constraintValue)
            throw new ValidationException(buildMessage(formatter, field,
constraintValue));
    }

    private String buildMessage(MessageFormatter formatter, Field field,
Integer constraintValue) {
        return formatter.format(constraintValue, field.getLabel());
    }
    
    @Override
    public void render(Field field, Integer constraintValue,
MessageFormatter formatter, MarkupWriter writer,
            FormSupport formSupport) {
        formSupport.addValidation(field, "maxprecision",
buildMessage(formatter, field, constraintValue), constraintValue);
    }

}

//AppModule

    @SuppressWarnings("rawtypes")
    public static void
contributeFieldValidatorSource(MappedConfiguration<String, Validator>
configuration) {
        configuration.add("maxScale", new MaxScale());
        configuration.add("maxPrecision", new MaxPrecision());
    }


--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/Validate-BigDecimal-maxlength-not-working-tp5713611p5713639.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to