Search in sources :

Example 1 with VisitorFieldValidator

use of com.opensymphony.xwork2.validator.validators.VisitorFieldValidator in project struts by apache.

the class Form method findFieldValidators.

private void findFieldValidators(String name, Class actionClass, String actionName, List<Validator> validatorList, List<Validator> resultValidators, String prefix) {
    for (Validator validator : validatorList) {
        if (validator instanceof FieldValidator) {
            FieldValidator fieldValidator = (FieldValidator) validator;
            if (validator instanceof VisitorFieldValidator) {
                VisitorFieldValidator vfValidator = (VisitorFieldValidator) fieldValidator;
                Class clazz = getVisitorReturnType(actionClass, vfValidator.getFieldName());
                if (clazz == null) {
                    continue;
                }
                List<Validator> visitorValidators = actionValidatorManager.getValidators(clazz, actionName);
                String vPrefix = prefix + (vfValidator.isAppendPrefix() ? vfValidator.getFieldName() + "." : "");
                findFieldValidators(name, clazz, actionName, visitorValidators, resultValidators, vPrefix);
            } else if ((prefix + fieldValidator.getFieldName()).equals(name)) {
                if (StringUtils.isNotBlank(prefix)) {
                    // fixing field name for js side
                    FieldVisitorValidatorWrapper wrap = new FieldVisitorValidatorWrapper(fieldValidator, prefix);
                    resultValidators.add(wrap);
                } else {
                    resultValidators.add(fieldValidator);
                }
            }
        }
    }
}
Also used : VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)

Example 2 with VisitorFieldValidator

use of com.opensymphony.xwork2.validator.validators.VisitorFieldValidator in project struts by apache.

the class ActionValidatorManagerTest method testValidate.

public void testValidate() throws Exception {
    /* MockAction.class */
    // reference number
    ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
    final RequiredStringValidator referenceNumberRequiredStringValidator = container.inject(RequiredStringValidator.class);
    referenceNumberRequiredStringValidator.setFieldName("referenceNumber");
    referenceNumberRequiredStringValidator.setDefaultMessage("Reference number is required");
    referenceNumberRequiredStringValidator.setValueStack(stack);
    // order
    final RequiredFieldValidator orderRequiredValidator = container.inject(RequiredFieldValidator.class);
    orderRequiredValidator.setFieldName("order");
    orderRequiredValidator.setDefaultMessage("Order is required");
    orderRequiredValidator.setValueStack(stack);
    // customer
    final RequiredFieldValidator customerRequiredValidator = container.inject(RequiredFieldValidator.class);
    customerRequiredValidator.setFieldName("customer");
    customerRequiredValidator.setDefaultMessage("Customer is required");
    customerRequiredValidator.setValueStack(stack);
    final VisitorFieldValidator customerVisitorValidator = container.inject(VisitorFieldValidator.class);
    customerVisitorValidator.setAppendPrefix(true);
    customerVisitorValidator.setFieldName("customer");
    customerVisitorValidator.setValueStack(stack);
    /* Customer.class */
    // customer -> name
    final RequiredStringValidator customerNameRequiredStringValidator = container.inject(RequiredStringValidator.class);
    customerNameRequiredStringValidator.setFieldName("name");
    customerNameRequiredStringValidator.setDefaultMessage("Name is required");
    customerNameRequiredStringValidator.setValueStack(stack);
    // customer -> age
    final RequiredFieldValidator customerAgeRequiredValidator = container.inject(RequiredFieldValidator.class);
    customerAgeRequiredValidator.setFieldName("age");
    customerAgeRequiredValidator.setDefaultMessage("Age is required");
    customerAgeRequiredValidator.setValueStack(stack);
    // customer -> Address
    final RequiredFieldValidator customerAddressRequiredFieldValidator = container.inject(RequiredFieldValidator.class);
    customerAddressRequiredFieldValidator.setFieldName("address");
    customerAddressRequiredFieldValidator.setDefaultMessage("Address is required");
    customerAddressRequiredFieldValidator.setValueStack(stack);
    final VisitorFieldValidator customerAddressVisitorFieldValidator = container.inject(VisitorFieldValidator.class);
    customerAddressVisitorFieldValidator.setFieldName("address");
    customerAddressVisitorFieldValidator.setAppendPrefix(true);
    // customerAddressVisitorFieldValidator.setDefaultMessage("");
    customerAddressVisitorFieldValidator.setValueStack(stack);
    /* Address.class */
    // customer -> Address -> street
    final RequiredStringValidator customerAddressStreetRequiredFieldValidator = container.inject(RequiredStringValidator.class);
    customerAddressStreetRequiredFieldValidator.setFieldName("street");
    customerAddressStreetRequiredFieldValidator.setDefaultMessage("Street is required");
    customerAddressStreetRequiredFieldValidator.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator.setValueStack(stack);
    final RequiredStringValidator customerAddressStreetRequiredFieldValidator2 = container.inject(RequiredStringValidator.class);
    customerAddressStreetRequiredFieldValidator2.setFieldName("street");
    customerAddressStreetRequiredFieldValidator2.setDefaultMessage("Street is required 2");
    customerAddressStreetRequiredFieldValidator2.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator2.setValueStack(stack);
    // customer -> Address -> pobox
    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator = container.inject(RequiredStringValidator.class);
    customerAddressPoboxRequiredFieldValidator.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator.setDefaultMessage("PO Box is required");
    customerAddressPoboxRequiredFieldValidator.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator.setValueStack(stack);
    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator2 = container.inject(RequiredStringValidator.class);
    customerAddressPoboxRequiredFieldValidator2.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator2.setDefaultMessage("PO Box is required 2");
    customerAddressPoboxRequiredFieldValidator2.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator2.setValueStack(stack);
    final List<Validator> validatorsForMockAction = new ArrayList<Validator>() {

        {
            add(referenceNumberRequiredStringValidator);
            add(orderRequiredValidator);
            add(customerRequiredValidator);
            add(customerVisitorValidator);
        }
    };
    final List<Validator> validatorsForCustomer = new ArrayList<Validator>() {

        {
            add(customerNameRequiredStringValidator);
            add(customerAgeRequiredValidator);
            add(customerAddressRequiredFieldValidator);
            add(customerAddressVisitorFieldValidator);
        }
    };
    final List<Validator> validatorsForAddress = new ArrayList<Validator>() {

        {
            add(customerAddressStreetRequiredFieldValidator);
            add(customerAddressStreetRequiredFieldValidator2);
            add(customerAddressPoboxRequiredFieldValidator);
            add(customerAddressPoboxRequiredFieldValidator2);
        }
    };
    DefaultActionValidatorManager validatorManager = new DefaultActionValidatorManager() {

        @Override
        public List<Validator> getValidators(Class clazz, String context, String method) {
            if (clazz.isAssignableFrom(MockAction.class)) {
                return validatorsForMockAction;
            } else if (clazz.isAssignableFrom(Customer.class)) {
                return validatorsForCustomer;
            } else if (clazz.isAssignableFrom(Address.class)) {
                return validatorsForAddress;
            }
            return Collections.emptyList();
        }
    };
    container.inject(validatorManager);
    customerVisitorValidator.setActionValidatorManager(validatorManager);
    customerAddressVisitorFieldValidator.setActionValidatorManager(validatorManager);
    MockAction action = container.inject(MockAction.class);
    stack.push(action);
    validatorManager.validate(action, "ctx");
    assertFalse(action.hasActionErrors());
    assertFalse(action.hasActionMessages());
    assertTrue(action.hasFieldErrors());
    assertTrue(action.getFieldErrors().containsKey("referenceNumber"));
    assertEquals((action.getFieldErrors().get("referenceNumber")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("order"));
    assertEquals((action.getFieldErrors().get("order")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.name"));
    assertEquals((action.getFieldErrors().get("customer.name")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.age"));
    assertEquals((action.getFieldErrors().get("customer.age")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.street"));
    assertEquals((action.getFieldErrors().get("customer.address.street")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.pobox"));
    assertEquals((action.getFieldErrors().get("customer.address.pobox")).size(), 2);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory) ArrayList(java.util.ArrayList) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)

Aggregations

VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)1 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)1 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)1 ArrayList (java.util.ArrayList)1