| 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.ioc.internal.util.InternalUtils; |
| 22 | |
import org.apache.tapestry5.services.FormSupport; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 752 | public final class Required extends AbstractValidator<Void, Object> |
| 28 | |
{ |
| 29 | |
public Required() |
| 30 | |
{ |
| 31 | 8 | super(null, Object.class, "required"); |
| 32 | 8 | } |
| 33 | |
|
| 34 | |
public void validate(Field field, Void constraintValue, MessageFormatter formatter, Object value) |
| 35 | |
throws ValidationException |
| 36 | |
{ |
| 37 | 228 | if (value == null || InternalUtils.isBlank(value.toString())) |
| 38 | 32 | throw new ValidationException(buildMessage(formatter, field)); |
| 39 | 196 | } |
| 40 | |
|
| 41 | |
private String buildMessage(MessageFormatter formatter, Field field) |
| 42 | |
{ |
| 43 | 562 | return formatter.format(field.getLabel()); |
| 44 | |
} |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public boolean isRequired() |
| 50 | |
{ |
| 51 | 694 | return true; |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer, |
| 55 | |
FormSupport formSupport) |
| 56 | |
{ |
| 57 | 530 | formSupport.addValidation(field, "required", buildMessage(formatter, field), null); |
| 58 | 530 | } |
| 59 | |
} |