Question

This is really weird. I am doing validation in a js library. I check if fields are blank or null and throw an error if they are.

I have a date field that I want to check. If I do not put in a default value, the code works fine. It says the date is blank and then when I put in a value it lets it pass. But if I put in a default of @Now or @Today, it will not pick up the error if the user happened to erase the date. I know it is not really necessary - I could put a required validator on, but it is driving me crazy that I cannot figure this out.

    //This SSJS script library consolidates all the validation in one place
    //The postValidationError() function flags a control as invalid and provides an error message
    //so that the XPages ErrorMessage control is used to display the error on the page.

    var validateForm = function(){
        var valid = true;
        var control;
        var val;


        // ***  REPEAT THE FOLLOWING BLOCK OF CODE FOR EACH CONTROL FOR BASIC "REQUIRED" VALIDATION

        // For each field, change the Control Name in getComponent() and the error message text in postValidationError()
        //   Optionally, modify the IF conditions with more complex JavaScript for value ranges, regular expressions, data lookups, etc.


        //Validate Location
        control = getComponent("loc");
        val = control.getValue();
        if (isEmpty(val)) {
            valid = false;
            postValidationError(control,"Please enter a Loc");
        }


        //Validate Work Category
        control = getComponent("workCategory");
        val = control.getValue();
        if (isEmpty(val)) {
            valid = false;
            postValidationError(control,"Please enter a Work Category");
        }

        //Validate Work Sub Category
        control = getComponent("workSubCategory");
        val = control.getValue();
        if (isEmpty(val)) {
            valid = false;
            postValidationError(control,"Please enter a Sub Work Category");
        }


        //Validate Date


        control = getComponent("date");
        val = control.getValue();
        if (isEmpty(val)) {
            valid = false;
            postValidationError(control,"Please enter a date");
        }
        //Validate Time Spent
        control = getComponent("timeSpent");
        val = control.getValue();
        if (isEmpty(val)) {
            valid = false;
            postValidationError(control,"Please enter Time Spent");
        }

        // *** ----------------------------------------------------------------   *** 

        return valid;
    }

    function postValidationError(control, msg) {
        if ((typeof msg) != "string")
            return;
        var msgObj = new javax.faces.application.FacesMessage(javax.faces.application.FacesMessage.SEVERITY_ERROR, msg, msg);
        facesContext.addMessage(control.getClientId(facesContext), msgObj);
        control.setValid(false);
    }

    function isEmpty(o){


        return (o == null || o == "") ? true: false;

        //return (o == null || @Trim($A(o)[0]) == "" ) ? true : false;
    }

    function $A( object ){
        try {
            if( typeof object === 'undefined' || object === null ){ return []; }
            if( typeof object === 'string' ){ return [ object ]; }
            if( typeof object.toArray !== 'undefined' ){return object.toArray();}
            if( object.constructor === Array ){ return object; }  
            return [ object ];
        } catch( e ) {  }
    }
Was it helpful?

Solution

Bryan, the recommended way in XPages for validation is to use a validator, You write much less code, you can selectively control when to validate fields, you can separate different checks from each other.

Check my thoughts about validation. In a nutshell:

  • Validation in code (a button, the submit event etc.) is a typical way validation is done. Being the prevalent way doesn't make it right . You need to roll your own notification mechanism (like updating a label) and tend to tie your validation into the UI. Also when you remove a field the validation routine is likely to break. Last not least: you have a hard time documenting what gets validated and why. (You see where I'm going with that)
  • Validators are defined together with a field and open a series of possibilities. XPages offers 9 different validators.
  • You can write JavaScript, regular expressions, check for data types or roll your very own. All you can do in a button/event code you can do in a validator. Since the validators themselves don't interact with the UI the designer can decide how to surface the messages without changes to the validation code. When you remove a field all its validation code goes with it, so maintenance gets much easier. Last not least: you can run an XSLT report against your XPages source and render a report that shows a field with all the defined validators, which makes documentation easier.
  • Form Validation are the @Formulas defined in your classic Notes form. They only fire when you have specified "Run form validation" as "On Save" or "Both". Typically you would use those when upgrading existing applications.

Extracted from another blog entry

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