| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.validator; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.Field; |
| 18 | |
import org.apache.tapestry5.MarkupWriter; |
| 19 | |
import org.apache.tapestry5.ValidationException; |
| 20 | |
import org.apache.tapestry5.ioc.MessageFormatter; |
| 21 | |
import org.apache.tapestry5.services.FormSupport; |
| 22 | |
|
| 23 | 0 | public final class MaxLength extends AbstractValidator<Integer, String> |
| 24 | |
{ |
| 25 | |
public MaxLength() |
| 26 | |
{ |
| 27 | 6 | super(Integer.class, String.class, "maximum-string-length"); |
| 28 | 6 | } |
| 29 | |
|
| 30 | |
public void validate(Field field, Integer constraintValue, MessageFormatter formatter, String value) |
| 31 | |
throws ValidationException |
| 32 | |
{ |
| 33 | 4 | if (value.length() > constraintValue) |
| 34 | 2 | throw new ValidationException(buildMessage(formatter, field, constraintValue)); |
| 35 | 2 | } |
| 36 | |
|
| 37 | |
private String buildMessage(MessageFormatter formatter, Field field, Integer constraintValue) |
| 38 | |
{ |
| 39 | 2 | return formatter.format(constraintValue, field.getLabel()); |
| 40 | |
} |
| 41 | |
|
| 42 | |
public void render(Field field, Integer constraintValue, MessageFormatter formatter, MarkupWriter writer, |
| 43 | |
FormSupport formSupport) |
| 44 | |
{ |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 0 | formSupport.addValidation(field, "maxlength", buildMessage(formatter, field, constraintValue), constraintValue); |
| 49 | 0 | } |
| 50 | |
} |