2

Let's assume that there a field of type BigDecimal in an action class as follows.

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "struts-default")
public final class TestAction extends ActionSupport
{
    private BigDecimal price;

    //Setter and getter.        

    @Validations(
    requiredFields = {
        @RequiredFieldValidator(fieldName = "price", type = ValidatorType.FIELD, message = "Price is mandatory.")},
    fieldExpressions = {
        @FieldExpressionValidator(fieldName = "price", expression = "price>0", shortCircuit = true, message = "Price cannot be less than or equal to zero.")})
    @Action(value = "Add",
    results = {
        @Result(name = ActionSupport.SUCCESS, type = "redirectAction", params = {"namespace", "/admin_side", "actionName", "Test"}),
        @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "price", "validation.validateAnnotatedMethodOnly", "true"})
    })
    public String insert() {
        return ActionSupport.SUCCESS;
    }

    //This method is worth nothing. It is used just to return an initial view on page load.
    @Action(value = "Test",
    results = {
        @Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
        @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "", "params.excludeMethods", "load", "validation.validateAnnotatedMethodOnly", "true"})})
    public String load() throws Exception {
        return ActionSupport.SUCCESS;
    }
}

And following is the form.

<s:form namespace="/admin_side" action="Test" id="dataForm" name="dataForm">
    <s:fielderror fieldName="price"/>
    <s:textfield id="price" name="price"/>

    <s:submit value="Submit" action="Add"/>
</s:form>

I want to achieve,

  1. If the field is left blank then, the only message, Price is mandatory. should be displayed through @RequiredFieldValidator
  2. If a non-numeric value like "abc" is entered then, it should only display the conversion error message from a property file.
  3. If a negative value is attempted then, the only message, Price cannot be less than or equal to zero. should appear through @FieldExpressionValidator.

Either one conversion error or one validation error should appear at a time.

Is this possible? I do by far not understand properly the function of the shourtCircuit attribute.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Do you only display errors or do you want to prevent validations? – Roman C Jan 10 '14 at 18:29
  • Yes I'm talking about prevention. Only one at a time should occur. The rest must be ignored. If conversion fails then, there is no meaning to check for any validation(s). If conversion succeeds then, the validations should be performed in a defined order, first empty fields should be validated, if it succeeds then, the value should be checked for a range/length and so on. If one validation fails then, the rest must be ignored. Can this be done? – Tiny Jan 10 '14 at 18:34

1 Answers1

2

At the first glance haven't seen so much. But looking at the Type Conversion Error Handling I'd say that there's a way to handle conversion errors. By adding a conversion validator to the configuration which is short-circuit validator. Short-circuit means that if such validator has errors other validators are skipped.

 conversionErrorFields = @ConversionErrorFieldValidator(fieldName = "price", message = "Price has invalid value", shortCircuit = true) 

place this code under @Validations annotation.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I'm currently at a different location and cannot try it. I will try it later (two or more days later). Thanks. – Tiny Jan 11 '14 at 17:18
  • **Off topic:** May I know how can you have 41 votes a day (at least until now today)? The daily vote limit is 40 (maximum). – Tiny Jan 11 '14 at 17:34
  • I don't know some day I thought it depend on rep but as of rep grow up the limit didn't change, I think that it has some extra votes over limit. – Roman C Jan 11 '14 at 17:52