Example usage for org.apache.commons.validator Field getArg

List of usage examples for org.apache.commons.validator Field getArg

Introduction

In this page you can find the example usage for org.apache.commons.validator Field getArg.

Prototype

public Arg getArg(int position) 

Source Link

Document

Gets the default Arg object at the given position.

Usage

From source file:cn.com.ihuimobile.test.validate.ValidateExample.java

/**
 * Dumps out the Bean in question and the results of validating it.
 *//*from   w  ww .j a  v a  2s .  com*/
@SuppressWarnings("unchecked")
public static void printResults(ValidateBean bean, ValidatorResults results, ValidatorResources resources) {

    boolean success = true;

    // Start by getting the form for the current locale and Bean.
    Form form = resources.getForm(Locale.getDefault(), "ValidateBean");

    System.out.println("\n\nValidating:");
    System.out.println(bean);

    // Iterate over each of the properties of the Bean which had messages.
    Iterator<String> propertyNames = results.getPropertyNames().iterator();
    while (propertyNames.hasNext()) {
        String propertyName = propertyNames.next();

        // Get the Field associated with that property in the Form
        Field field = form.getField(propertyName);

        // Look up the formatted name of the field from the Field arg0
        String prettyFieldName = apps.getString(field.getArg(0).getKey());

        // Get the result of validating the property.
        ValidatorResult result = results.getValidatorResult(propertyName);

        // Get all the actions run against the property, and iterate over their names.
        Iterator<String> keys = result.getActions();
        while (keys.hasNext()) {
            String actName = keys.next();

            // Get the Action for that name.
            ValidatorAction action = resources.getValidatorAction(actName);

            // If the result is valid, print PASSED, otherwise print FAILED
            System.out.println(propertyName + "[" + actName + "] ("
                    + (result.isValid(actName) ? "PASSED" : "FAILED") + ")");

            //If the result failed, format the Action's message against the formatted field name
            if (!result.isValid(actName)) {
                success = false;
                String message = apps.getString(action.getMsg());
                Object[] args = { prettyFieldName };
                System.out.println("     Error message will be: " + MessageFormat.format(message, args));

            }
        }
    }
    if (success) {
        System.out.println("FORM VALIDATION PASSED");
    } else {
        System.out.println("FORM VALIDATION FAILED");
    }

}

From source file:net.sourceforge.fenixedu.presentationTier.validator.form.ValidateMultiBox.java

public static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext application) {

    try {/*from w  ww.j a v  a  2  s  .  c o  m*/

        DynaActionForm form = (DynaActionForm) bean;

        String sProperty = field.getProperty();
        Object[] multiBox = (Object[]) form.get(sProperty);

        String feminin = field.getVarValue("femininProperty");
        if ((multiBox != null) && (multiBox.length > 0)) {
            return true;
        }
        if (feminin != null && feminin.equalsIgnoreCase("true")) {
            errors.add(field.getKey(), new ActionError("errors.required.a.checkbox", field.getArg(0).getKey()));
        } else {
            errors.add(field.getKey(), new ActionError("errors.required.checkbox", field.getArg(0).getKey()));
        }

        return false;

    } catch (Exception e) {
        errors.add(field.getKey(),
                new ActionError("errors.required.undefined.checkbox", field.getArg(0).getKey()));
        return false;
    }

}

From source file:net.sourceforge.fenixedu.presentationTier.validator.form.ValidateMultiBoxWithSelectAllCheckBox.java

public static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext application) {

    try {/*from w  ww .j av  a2s  . co m*/

        DynaActionForm form = (DynaActionForm) bean;
        Boolean selectAllBox = (Boolean) form.get(field.getProperty());
        String sProperty2 = field.getVarValue("secondProperty");
        String[] multiBox = (String[]) form.get(sProperty2);

        if (((selectAllBox != null) && selectAllBox.booleanValue())
                || (multiBox != null) && (multiBox.length > 0)) {
            return true;
        }
        errors.add(field.getKey(), new ActionMessage("errors.required.checkbox", field.getArg(0).getKey()));
        return false;

    } catch (Exception e) {
        errors.add(field.getKey(), new ActionMessage("errors.required.checkbox", field.getArg(0).getKey()));
        return false;
    }

}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java

/**
 * wIndextB?[h?B//w w  w.j  a va  2 s .c  om
 * 
 * <b>?\bhANVtH?[? tH?[O?Ap?s???B</b>
 * 
 * @param field
 *            utB?[hIuWFNg
 * @param pos
 *            ?CfbNX
 * @return u?tB?[hIuWFNg
 */
protected static Field getArrayIndexField(Field field, int pos) {
    Field cloneField = (Field) field.clone();

    // ##index?A?vf??
    Arg argParam = null;
    String argStr = null;

    argParam = cloneField.getArg(0);
    if (argParam != null) {
        argStr = argParam.getKey();
        cloneField.getArg(0).setKey(replaceIndexString(argStr, pos + 1));
    }

    argParam = cloneField.getArg(1);
    if (argParam != null) {
        argStr = argParam.getKey();
        cloneField.getArg(1).setKey(replaceIndexString(argStr, pos + 1));
    }

    argParam = cloneField.getArg(2);
    if (argParam != null) {
        argStr = argParam.getKey();
        cloneField.getArg(2).setKey(replaceIndexString(argStr, pos + 1));
    }

    argParam = cloneField.getArg(3);
    if (argParam != null) {
        argStr = argParam.getKey();
        cloneField.getArg(3).setKey(replaceIndexString(argStr, pos + 1));
    }

    return cloneField;
}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksExTest01.java

/**
 * testGetArrayIndexField01()/* w  w  w . j  av  a  2  s . c  om*/
 * <br><br>
 *
 * (??n)
 * <br>
 * _?FF
 * <br><br>
 * l?F(?) field:not null<br>
 *                ?Arg??<br>
 *         (?) pos:0<br>
 *
 * <br>
 * l?F(l) Field:not null<br>
 *                  ?i??j<br>
 *
 * <br>
 * ?fieldArgCX^X????A
 * fieldmF?B
 * <br>
 *
 * @throws Exception ?\bh?O
 */
public void testGetArrayIndexField01() throws Exception {

    // l?
    Field field = new Field();

    // eXg?s
    Field retField = FieldChecksEx.getArrayIndexField(field, 0);
    assertNull(retField.getArg(0));
    assertNull(retField.getArg(1));
    assertNull(retField.getArg(2));
    assertNull(retField.getArg(3));
}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksExTest01.java

/**
 * testGetArrayIndexField03()//  w  w w. ja v a 2  s.co m
 * <br><br>
 *
 * (??n)
 * <br>
 * _?FF
 * <br><br>
 * l?F(?) field:not null<br>
 *                {Arg:key="##INDEX", position=0;<br>
 *                 Arg:key="##INDEX", position=1;<br>
 *                 Arg:key="##INDEX", position=2;<br>
 *                 Arg:key="##INDEX", position=3}<br>
 *         (?) pos:0<br>
 *
 * <br>
 * l?F(l) Field:not null<br>
 *                  {Arg:key="1", position=0;<br>
 *                   Arg:key="1", position=1;<br>
 *                   Arg:key="1", position=2;<br>
 *                   Arg:key="1", position=3}<br>
 *
 * <br>
 * ?field?ArgCX^Xkeyl"##INDEX"???A
 * keyl?posl?{PmF?B
 * <br>
 *
 * @throws Exception ?\bh?O
 */
public void testGetArrayIndexField03() throws Exception {

    // l?
    Field field = new Field();
    // arg?
    Arg param0 = new Arg();
    param0.setKey("##INDEX");
    param0.setPosition(0);
    field.addArg(param0);

    Arg param1 = new Arg();
    param1.setKey("##INDEX");
    param1.setPosition(1);
    field.addArg(param1);

    Arg param2 = new Arg();
    param2.setKey("##INDEX");
    param2.setPosition(2);
    field.addArg(param2);

    Arg param3 = new Arg();
    param3.setKey("##INDEX");
    param3.setPosition(3);
    field.addArg(param3);

    // eXg?s
    Field retField = FieldChecksEx.getArrayIndexField(field, 0);

    assertEquals("1", retField.getArg(0).getKey());
    assertEquals("1", retField.getArg(1).getKey());
    assertEquals("1", retField.getArg(2).getKey());
    assertEquals("1", retField.getArg(3).getKey());
}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksExTest01.java

/**
 * testGetArrayIndexField04()/*from   w w  w . j  a va 2 s.  c  o  m*/
 * <br><br>
 *
 * (??n)
 * <br>
 * _?FF
 * <br><br>
 * l?F(?) field:not null<br>
 *                {Arg:key="##INDEX", position=0;<br>
 *                 Arg:key="arg1", position=1;<br>
 *                 Arg:key="arg2", position=2;<br>
 *                 Arg:key="##INDEX", position=3}<br>
 *         (?) pos:0<br>
 *
 * <br>
 * l?F(l) Field:not null<br>
 *                  {Arg:key="1", position=0;<br>
 *                   Arg:key="arg1", position=1;<br>
 *                   Arg:key="arg2", position=2;<br>
 *                   Arg:key="1", position=3}<br>
 *
 * <br>
 * ?field?ArgCX^Xkeyl"##INDEX"???A
 * keyl?posl?{PmF?B
 * <br>
 *
 * @throws Exception ?\bh?O
 */
public void testGetArrayIndexField04() throws Exception {

    // l?
    Field field = new Field();
    // arg?
    Arg param0 = new Arg();
    param0.setKey("##INDEX");
    param0.setPosition(0);
    field.addArg(param0);

    Arg param1 = new Arg();
    param1.setKey("arg1");
    param1.setPosition(1);
    field.addArg(param1);

    Arg param2 = new Arg();
    param2.setKey("arg2");
    param2.setPosition(2);
    field.addArg(param2);

    Arg param3 = new Arg();
    param3.setKey("##INDEX");
    param3.setPosition(3);
    field.addArg(param3);

    // eXg?s
    Field retField = FieldChecksEx.getArrayIndexField(field, 0);

    assertEquals("1", retField.getArg(0).getKey());
    assertEquals("arg1", retField.getArg(1).getKey());
    assertEquals("arg2", retField.getArg(2).getKey());
    assertEquals("1", retField.getArg(3).getKey());
}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksExTest01.java

/**
 * testGetArrayIndexField02()/* w  ww.  j  av  a 2 s  .c  o  m*/
 * <br><br>
 *
 * (??n)
 * <br>
 * _?FF
 * <br><br>
 * l?F(?) field:not null<br>
 *                {Arg:key="arg0", position=0;<br>
 *                 Arg:key="arg1", position=1;<br>
 *                 Arg:key="arg2", position=2;<br>
 *                 Arg:key="arg3", position=3}<br>
 *         (?) pos:0<br>
 *
 * <br>
 * l?F(l) Field:not null<br>
 *                  {Arg:key="arg0", position=0;<br>
 *                   Arg:key="arg1", position=1;<br>
 *                   Arg:key="arg2", position=2;<br>
 *                   Arg:key="arg3", position=3}<br>
 *                  (?)<br>
 *
 * <br>
 * ?field?ArgCX^Xkeyl"##INDEX"???A
 * fieldmF?B
 * <br>
 *
 * @throws Exception ?\bh?O
 */
public void testGetArrayIndexField02() throws Exception {

    // l?
    Field field = new Field();
    // arg?
    Arg param0 = new Arg();
    param0.setKey("arg0");
    param0.setPosition(0);
    field.addArg(param0);

    Arg param1 = new Arg();
    param1.setKey("arg1");
    param1.setPosition(1);
    field.addArg(param1);

    Arg param2 = new Arg();
    param2.setPosition(2);
    param2.setKey("arg2");
    field.addArg(param2);

    Arg param3 = new Arg();
    param3.setPosition(3);
    param3.setKey("arg3");
    field.addArg(param3);

    // eXg?s
    Field retField = FieldChecksEx.getArrayIndexField(field, 0);

    assertEquals("arg0", retField.getArg(0).getKey());
    assertEquals("arg1", retField.getArg(1).getKey());
    assertEquals("arg2", retField.getArg(2).getKey());
    assertEquals("arg3", retField.getArg(3).getKey());
}

From source file:org.apache.commons.validator.example.ValidateExample.java

/**
 * Dumps out the Bean in question and the results of validating it.
 */// w  ww .j a v  a2  s .  c  o  m
public static void printResults(Object bean, ValidatorResults results, ValidatorResources resources) {

    boolean success = true;

    // Start by getting the form for the current locale and Bean.
    Form form = resources.getForm(Locale.getDefault(), "ValidateBean");

    System.out.println("\n\nValidating:");
    System.out.println(bean);

    // Iterate over each of the properties of the Bean which had messages.
    Iterator propertyNames = results.getPropertyNames().iterator();
    while (propertyNames.hasNext()) {
        String propertyName = (String) propertyNames.next();

        // Get the Field associated with that property in the Form
        Field field = form.getField(propertyName);

        // Look up the formatted name of the field from the Field arg0
        String prettyFieldName = apps.getString(field.getArg(0).getKey());

        // Get the result of validating the property.
        ValidatorResult result = results.getValidatorResult(propertyName);

        // Get all the actions run against the property, and iterate over their names.
        Map actionMap = result.getActionMap();
        Iterator keys = actionMap.keySet().iterator();
        while (keys.hasNext()) {
            String actName = (String) keys.next();

            // Get the Action for that name.
            ValidatorAction action = resources.getValidatorAction(actName);

            // If the result is valid, print PASSED, otherwise print FAILED
            System.out.println(propertyName + "[" + actName + "] ("
                    + (result.isValid(actName) ? "PASSED" : "FAILED") + ")");

            //If the result failed, format the Action's message against the formatted field name
            if (!result.isValid(actName)) {
                success = false;
                String message = apps.getString(action.getMsg());
                Object[] args = { prettyFieldName };
                System.out.println("     Error message will be: " + MessageFormat.format(message, args));

            }
        }
    }
    if (success) {
        System.out.println("FORM VALIDATION PASSED");
    } else {
        System.out.println("FORM VALIDATION FAILED");
    }

}

From source file:org.hyperic.util.validator.common.CommonValidator.java

/**
 * Perform all form-specific the validation. All form specific validation
 * is performed and then finally a CommonValidatorException object will be
 * thrown if (AND ONLY IF) validation errors were detected. The exception
 * object will contain a collection of error Strings see
 * {@ref CommonValidatorException}./*  ww  w  . ja v a 2  s  .  c om*/
 *
 * @param validationMappingRes A String containing the name of the
 * mapping resource file.
 * @param formName A String containing the name of the form containing
 * the validation action references.
 * @param beanInstance An instance of the bean to apply validation on.
 * @throws CommonValidatorException - Exception containing collection of
 * messages.
 **/
public void validate(String validationMappingRes, String formName, Object beanInstance)
        throws CommonValidatorException, SAXException {
    InputStream xmlStream = null;
    CommonValidatorException cve = null;
    ValidatorResults results;

    try {
        // Start by setting the "ValidatorResources" object. Its only
        // created if necessary. Contains FormSets stored against locale.
        setValidatorResources(validationMappingRes, beanInstance, xmlStream);

        // Get the form for the current locale and Bean.
        Form form = _validatorResources.getForm(Locale.getDefault(), formName);

        // Instantiate the validator (coordinates the validation
        // while the ValidatorResources implements the validation)
        Validator validator = new Validator(_validatorResources, formName);

        // Tell the validator which bean to validate against.
        validator.setParameter(Validator.BEAN_PARAM, beanInstance);

        // Get the results
        results = validator.validate();

        // Localize a reference for future access.
        setValidatorResults(results);

        // Iterate over each of the properties of the Bean which had messages.
        Iterator propertyNames = results.getPropertyNames().iterator();

        while (propertyNames.hasNext()) {
            // There were errors. Instantiate CVE

            String propertyName = (String) propertyNames.next();

            // Get the Field associated with that property in the Form
            Field field = (Field) form.getField(propertyName);

            // Look up the formatted name of the field from the Field arg0
            String prettyFieldName = _properties.getString(field.getArg(0).getKey());

            // Get the result of validating the property.
            ValidatorResult result = results.getValidatorResult(propertyName);

            // Get all the actions run against the property, and iterate
            // over their names. Check for invalid results.
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            while (keys.hasNext()) {
                String actName = (String) keys.next();
                // Get the Action for that name.
                ValidatorAction action = _validatorResources.getValidatorAction(actName);
                if (!result.isValid(actName)) {
                    String message = _properties.getString(action.getMsg());
                    Object[] args = { prettyFieldName };
                    if (cve == null) {
                        cve = new CommonValidatorException();
                    }
                    cve.addMessage(MessageFormat.format(message, args));
                }
            }
        }
    } catch (IOException ex) {
        // Note: This exception shouldn't be reported to user since it
        // wasn't likely caused by user input.
        ex.printStackTrace(); //log this
    } catch (ValidatorException ex) {
        // Note: This exception shouldn't bubble up to user since it
        // wasn't likely caused by user input.
        ex.printStackTrace(); //log this
    } finally {
        // Make sure we close the input stream.
        if (xmlStream != null)
            try {
                xmlStream.close();
            } catch (Exception e) {
            }
        // Lastly, if we had any invalid fields, throw CVE.
        if (cve != null)
            throw cve;
    }
}