Package org.apache.commons.validator

Examples of org.apache.commons.validator.Field


    }

    public void testValidateWhenValid_SuccessWithDouble() throws Exception {
        MockValidationBean bean = new MockValidationBean();
        bean.setValue(-0.45);
        Field field = createFieldForProperty("value");
        field.addVar("test", "(*this* > -10.43)", "");

        MockControl control = MockControl.createControl(Errors.class);

        ValidatorAction validatorAction = new ValidatorAction();
        Errors errors = (Errors) control.getMock();
View Full Code Here


        assertTrue(result);
    }

    public void testValidateWhenValid_Failure() throws Exception {
        MockValidationBean bean = new MockValidationBean();
        Field field = createFieldForProperty("name");
        field.addVar("test", "(*this* != null)", "");

        MockControl control = MockControl.createControl(Errors.class);

        ValidatorAction validatorAction = new ValidatorAction();
        Errors errors = (Errors) control.getMock();
View Full Code Here

    }

    public void testValidateWhenValid_SuccessWithString() throws Exception {
        MockValidationBean bean = new MockValidationBean();
        bean.setName("uri");
        Field field = createFieldForProperty("name");
        field.addVar("test", "(*this* == 'uri')", "");

        MockControl control = MockControl.createControl(Errors.class);

        ValidatorAction validatorAction = new ValidatorAction();
        Errors errors = (Errors) control.getMock();
View Full Code Here

        boolean result = FieldChecks.validateValidWhen(bean, validatorAction, field, errors, validator);
        assertTrue(result);
    }

    private Field createFieldForProperty(String property) {
        Field field = new Field();
        field.setProperty(property);
        return field;
    }
View Full Code Here

        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 = propertyName; //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 = "invalid field"; // apps.getString(action.getMsg());
                    if (actName.equals("doubleRange"))
                    {
                        Arg f1 = field.getArg(1);
                        Arg f2 = field.getArg(2);
                        Arg f0 = field.getArg(0);
                        Object[] args =
                        { prettyFieldName, field.getVar("min").getValue(), field.getVar("max").getValue()};
                        System.out.println("     Error message will be: " + MessageFormat.format(message, args));
                    }
                    else
                    {
                        Object[] args =
View Full Code Here

            results.append(functionName);
            results.append(" () { \n");

            for (Iterator x = form.getFields().iterator(); x.hasNext();)
            {
                Field field = (Field)x.next();

                // Skip indexed fields for now until there is
                // a good way to handle error messages (and the length
                // of the list (could retrieve from scope?))
                if (field.isIndexed()
                    || field.getPage() != page
                    || !field.isDependency(va.getName()))
                {
                    continue;
                }

                String message = Resources.getMessage(app, request, messages,
                                                      locale, va, field);

                message = (message != null) ? message : "";

                //jscriptVar = this.getNextVar(jscriptVar);

                results.append("     this.a");
                results.append(jscriptVar++);
                results.append(" = new Array(\"");
                results.append(field.getKey()); // TODO: escape?
                results.append("\", \"");
                results.append(escapeJavascript(message));
                results.append("\", ");
                results.append("new Function (\"varName\", \"");

                Map vars = field.getVars();
                // Loop through the field's variables.
                Iterator varsIterator = vars.keySet().iterator();
                while (varsIterator.hasNext())
                {
                    String varName = (String)varsIterator.next(); // TODO: escape?
View Full Code Here

    {
        List actionMethods = new ArrayList();
        // Get List of actions for this Form
        for (Iterator i = form.getFields().iterator(); i.hasNext();)
        {
            Field field = (Field)i.next();
            for (Iterator x = field.getDependencyList().iterator(); x.hasNext();)
            {
                Object o = x.next();
                if (o != null && !actionMethods.contains(o))
                {
                    actionMethods.add(o);
View Full Code Here

        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 = propertyName; //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 = "invalid field"; // apps.getString(action.getMsg());
                    if (actName.equals("doubleRange"))
                    {
                        Arg f1 = field.getArg(1);
                        Arg f2 = field.getArg(2);
                        Arg f0 = field.getArg(0);
                        Object[] args =
                        { prettyFieldName, field.getVar("min").getValue(), field.getVar("max").getValue()};
                        System.out.println("     Error message will be: " + MessageFormat.format(message, args));
                    }
                    else
                    {
                        Object[] args =
View Full Code Here

                List lActions = new ArrayList();
                List lActionMethods = new ArrayList();

                // Get List of actions for this Form
                for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                    Field field = (Field) i.next();

                    for (Iterator x = field.getDependencyList().iterator();
                        x.hasNext();) {
                        Object o = x.next();

                        if (o != null && !lActionMethods.contains(o)) {
                            lActionMethods.add(o);
                        }
                    }

                }

                // Create list of ValidatorActions based on lActionMethods
                for (Iterator i = lActionMethods.iterator(); i.hasNext();) {
                    String depends = (String) i.next();
                    ValidatorAction va = resources.getValidatorAction(depends);

                    // throw nicer NPE for easier debugging
                    if (va == null) {
                        throw new NullPointerException(
                            "Depends string \""
                                + depends
                                + "\" was not found in validator-rules.xml.");
                    }

                    String javascript = va.getJavascript();
                    if (javascript != null && javascript.length() > 0) {
                        lActions.add(va);
                    } else {
                        i.remove();
                    }
                }

                Collections.sort(lActions, new Comparator() {
                    public int compare(Object o1, Object o2) {
                        ValidatorAction va1 = (ValidatorAction) o1;
                        ValidatorAction va2 = (ValidatorAction) o2;

                        if ((va1.getDepends() == null
                            || va1.getDepends().length() == 0)
                            && (va2.getDepends() == null
                            || va2.getDepends().length() == 0)) {
                            return 0;
                        } else if (
                            (va1.getDepends() != null
                            && va1.getDepends().length() > 0)
                            && (va2.getDepends() == null
                            || va2.getDepends().length() == 0)) {
                            return 1;
                        } else if (
                            (va1.getDepends() == null
                            || va1.getDepends().length() == 0)
                            && (va2.getDepends() != null
                            && va2.getDepends().length() > 0)) {
                            return -1;
                        } else {
                            return va1.getDependencyList().size() -
                              va2.getDependencyList().size();
                        }
                    }
                });

                String methods = null;
                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();

                    if (methods == null) {
                        methods = va.getMethod() + "(form)";
                    } else {
                        methods += " && " + va.getMethod() + "(form)";
                    }
                }

                results.append(getJavascriptBegin(methods));

                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();
                    String jscriptVar = null;
                    String functionName = null;

                    if (va.getJsFunctionName() != null
                        && va.getJsFunctionName().length() > 0) {
                        functionName = va.getJsFunctionName();
                    } else {
                        functionName = va.getName();
                    }

                    if (isStruts11()) {
                        results.append("    function " +
                          functionName + " () { \n");
                    } else {
                        results.append("    function " +
                          formName + "_" + functionName +
                          " () { \n");
                    }
                    for (Iterator x = form.getFields().iterator();
                        x.hasNext();) {
                        Field field = (Field) x.next();

                        // Skip indexed fields for now until there is a good
                        // way to handle error messages (and the length of the
                        // list (could retrieve from scope?))
                        if (field.isIndexed()
                            || field.getPage() != page
                            || !field.isDependency(va.getName())) {

                            continue;
                        }

                        String message =
                            Resources.getMessage(messages, locale, va, field);

                        message = (message != null) ? message : "";

                        jscriptVar = this.getNextVar(jscriptVar);

                        results.append(
                            "     this."
                                + jscriptVar
                                + " = new Array(\""
                                + getFormClientId()
                                + ":"
                                + field.getKey()
                                + "\", \""
                                + message
                                + "\", ");

                        results.append("new Function (\"varName\", \"");

                        Map vars = field.getVars();
                        // Loop through the field's variables.
                        Iterator varsIterator = vars.keySet().iterator();
                        while (varsIterator.hasNext()) {
                            String varName = (String) varsIterator.next();
                            Var var = (Var) vars.get(varName);
View Full Code Here

            results.append("    function " + jsFormName + "_" + functionName
                + " () { \n");

            for (Iterator x = form.getFields().iterator(); x.hasNext();) {
                Field field = (Field) x.next();

                // Skip indexed fields for now until there is a good way to
                // handle error messages (and the length of the list (could
                // retrieve from scope?))
                if (field.isIndexed() || (field.getPage() != page)
                    || !field.isDependency(va.getName())) {
                    continue;
                }

                String message =
                    Resources.getMessage(application, request, messages,
                        locale, va, field);

                message = (message != null) ? message : "";

                // prefix variable with 'a' to make it a legal identifier
                results.append("     this.a" + jscriptVar++ + " = new Array(\""
                    + field.getKey() + "\", \"" + escapeQuotes(message)
                    + "\", ");

                results.append("new Function (\"varName\", \"");

                Map vars = field.getVars();

                // Loop through the field's variables.
                Iterator varsIterator = vars.keySet().iterator();

                while (varsIterator.hasNext()) {
View Full Code Here

TOP

Related Classes of org.apache.commons.validator.Field

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.