Question

I have web flow with this view:

 <var name="newRecipe" class="springapp.service.NewRecipe" />

 <view-state id="displayNameView" view="/WEB-INF/jsp/add_name.jsp" model="newRecipe">
     <transition on="nameEntered" to="displayDescriptionView" />
 </view-state>

and validator:

@Component("newRecipeValidator")
public class NewRecipeValidator implements Validator {

    @Override
    public boolean supports(@SuppressWarnings("rawtypes") final Class clazz) {
        return NewRecipe.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(final Object obj, final Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.not-specified-name", "Pole wymagane.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "error.not-specified-description", "Pole wymagane.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category", "error.not-specified-category", "Pole wymagane.");
    }

    public void validateDisplayNameView(final NewRecipe obj, final ValidationContext context) {
        System.out.println("okej");
    }
}

My problem is that when web flows tries to validate view displayNameView it calls validateDisplayNameView() and after that validate().

In validate() (for view displayNameView) newRecipe always will have null fields description and category.

Was it helpful?

Solution

This is the default behavior when you implement Validator for view validation.

If you have overridden validate method in your Validator implementation and if validation is turned on in view-state (say eg., displayNameView), then first validateDisplayNameView method will be called followed by validate method.

I will override this default validate method only if some common validation is to be performed for all views in a flow. If not i will not override or keep it empty as:

    public void validate(Object obj, Errors err){};

You can spring web flow documentation mentioning the same here.

In your scenario, if you want to check the statements in validate method for displayNameView, move them to validateDisplayNameView method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top