You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Yasser Al Masri <ys...@yahoo.com> on 2006/02/04 07:01:37 UTC

Re: Validating a subset of the
element in validation.xml file

I have come up with a solution, and I'd like to share it with the community.  Any comments are welcomed.

This code is located inside one of my Action classes (or a super Action), where partial validation is needed:

    static Map validations;

    /**
     * @param mapping
     * @param request
     * @param form This is the form that is associated with the current action,          * and contains the fields to be validated. You should down-cast this to your          * form before using it in the method
     * @param fields Array of Strings that contains the names of the fields to be          * validated.
    private ActionErrors validateFields(ActionMapping mapping,
            HttpServletRequest request, ActionForm form, String[] fields,
            boolean replaceRequiredIf) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Field field;
        Iterator dependencies;
        String dependency;
        Validator validator = null;
        ValidatorAction action;
        ActionErrors errors = new ActionErrors();
        if (validations == null)
            validations = new HashMap();
        ValidatorResources resources = Resources.getValidatorResources(
                getServlet().getServletContext(), request);
        Map fieldMap = resources
                .get(getLocale(request), mapping.getAttribute()).getFieldMap();
        for (int i = 0; i < fields.length; i++) {
            field = (Field) fieldMap.get(fields[i]);
            for (dependencies = IteratorUtils.getIterator(field
                    .getDependencies()); dependencies.hasNext();) {
                dependency = (String) dependencies.next();
                if (!"required".equals(dependency)
                        && !"requiredif".equals(dependency)
                        && StringUtils.isBlank(ValidatorUtil.getValueAsString(
                                form, fields[i])))
                    continue;
                else {
                    action = resources.getValidatorAction(dependency);
                    if ("requiredif".equals(dependency)) {
                        if (replaceRequiredIf)
                            dependency = "required";
                        else {
                            dependency = "requiredIf";
                            if (validator == null)
                                validator = Resources.initValidator(mapping
                                        .getAttribute(), form, getServlet()
                                        .getServletContext(), request, errors,
                                        0);
                            applyValidator(new Class[] { Object.class,
                                    ValidatorAction.class, Field.class,
                                    ActionErrors.class, Validator.class,
                                    HttpServletRequest.class }, new Object[] {
                                    form, action, field, errors, validator,
                                    request }, dependency);
                            continue;
                        }
                    } else if ("minlength".equals(dependency))
                        dependency = "minLength";
                    else if ("maxlength".equals(dependency))
                        dependency = "maxLength";
                    applyValidator(
                            new Class[] { Object.class, ValidatorAction.class,
                                    Field.class, ActionErrors.class,
                                    HttpServletRequest.class },
                            new Object[] { form, action, field, errors, request },
                            dependency);
                }

            }
        }
        return errors;
    }

    private void applyValidator(Class[] types, Object[] params,
            String dependency) throws NoSuchMethodException,
            IllegalAccessException, InvocationTargetException {
        Method validator = (Method) validations.get(dependency);
        if (validator == null) {
            validator = FieldChecks.class.getMethod("validate"
                    + StringUtils.capitalize(dependency), types);
            validations.put(dependency.toLowerCase(), validator);
        }
        validator.invoke(null, params);
    }

Yasser Al Masri <ys...@yahoo.com> wrote:   Hi,

I have configured my form which contains 70 elements for validation using validation.xml file, and in many cases I needed to reuse the same exact definitions of the 's available within my  for validation of subsets of that form, e.g., I need to enable the user to fill in fields x, y, and z and submit the form to retrieve some information, so only fields x, y, and z has to be validated and not the whole 70 fields in the form, and in the same time, I need to utilize the rules I defined in validation.xml in the "depends" clause.

I have seen some utilizing the ValidatorResources, and Resources classes to do this, but I couldn't get an efficient working example. Does anybody have a code snippet to show me how to achieve this.

Your help is greatly appreciated.

 
  
---------------------------------
 
 What are the most popular cars? Find out at Yahoo! Autos 

		
---------------------------------
Relax. Yahoo! Mail virus scanning helps detect nasty viruses!