use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class ShortRangeFieldValidatorTest method prepareValidator.
private ShortRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
valueStack.push(action);
ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
validator.setValueStack(valueStack);
validator.setMaxExpression("${shortMaxValue}");
validator.setMinExpression("${shortMinValue}");
validator.setValidatorContext(context);
validator.setFieldName("shortRange");
validator.setDefaultMessage("Max is ${shortMaxValue}, min is ${shortMinValue} but value is ${shortRange}");
return validator;
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class ShortRangeFieldValidatorTest method testMaxValidation.
public void testMaxValidation() throws Exception {
// given
ValidationAction action = prepareAction((short) 11);
ValidatorContext context = new DummyValidatorContext(action, tpf);
ShortRangeFieldValidator validator = prepareValidator(action, context);
// when
validator.validate(action);
// then
assertTrue(context.getFieldErrors().size() == 1);
assertEquals("Max is 10, min is 2 but value is 11", context.getFieldErrors().get("shortRange").get(0));
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class ShortRangeFieldValidatorTest method testPassValidation.
public void testPassValidation() throws Exception {
// given
ValidationAction action = prepareAction((short) 5);
ValidatorContext context = new DummyValidatorContext(action, tpf);
ShortRangeFieldValidator validator = prepareValidator(action, context);
// when
validator.validate(action);
// then
assertTrue(context.getFieldErrors().size() == 0);
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class MethodNameExtractor method addValidationErrors.
private void addValidationErrors(ConstraintViolation[] violations, Object action, ValueStack valueStack, String parentFieldname) {
if (violations != null) {
ValidatorContext validatorContext = new DelegatingValidatorContext(action, textProviderFactory);
for (ConstraintViolation violation : violations) {
// translate message
String key = violation.getMessage();
String message;
// push context variable into stack, to allow use ${max}, ${min} etc in error messages
valueStack.push(violation.getMessageVariables());
// push the validator into the stack
valueStack.push(violation.getContext());
try {
message = validatorContext.getText(key);
} finally {
valueStack.pop();
valueStack.pop();
}
if (isActionError(violation)) {
LOG.debug("Adding action error '{}'", message);
validatorContext.addActionError(message);
} else {
ValidationError validationError = buildValidationError(violation.getContextPath(), message);
// build field name
String fieldName = validationError.getFieldName();
if (parentFieldname != null) {
fieldName = parentFieldname + "." + fieldName;
}
LOG.debug("Adding field error [{}] with message '{}'", fieldName, validationError.getMessage());
validatorContext.addFieldError(fieldName, validationError.getMessage());
// add violations of member object fields
addValidationErrors(violation.getCauses(), action, valueStack, fieldName);
}
}
}
}
Aggregations