locked
How to validate local property in beforeApplyChanges RRS feed

  • Question

  • The msdn link  Validate data on a screen (http://msdn.microsoft.com/en-us/library/jj733572.aspx#validate ) shows how to handle validate entity validations by using msls.BusinessObject.Details.Property which screen local property does not seem to have.  what is the work around to validate the local property by using msls.ValidationResult within beforeApplyChanges?

     
    new msls.ValidationResult(
            screen.Contact.details.properties.contactName,
            "Contact Name cannot contain the character '!'.")
            ];



    jl

    Tuesday, September 30, 2014 6:51 AM

Answers

  • Joe,

    You're correct, it appears beforeApplyChanges doesn't fire again while there are validation results on the screen.  That's weird, you'd think it would be a good place to clear validation results.

    Anyway, you can run validation as the user types by doing so in the input keyup handler like so:

    myapp.AddEditOrder.Property1_postRender = function (element, contentItem) {
    
        inputElement = $(element).find("input")[0];
        $(inputElement).keyup(function() {
            if (inputElement.value == 0) {
                //this line to show validation results immediately
                contentItem._alwaysShowValidationResults = true;
    
                //add validation results
                contentItem.validationResults = [
                    new msls.ValidationResult(
                        contentItem.screen.details.properties.Property1,
                        "Property1 cannot be '0'.")
                ];
            } else {
                //remove validation results
                contentItem.validationResults = [];
            }
        });
    };

    HTH,

    Josh


    • Edited by joshbooker Friday, October 3, 2014 1:54 PM
    • Marked as answer by Angie Xu Wednesday, October 8, 2014 1:59 AM
    Friday, October 3, 2014 1:53 PM

All replies

  • Hi Joe,

    You should be ale to uset hat code for local properties. This works for a local prop:

    myapp.AddEditOrder.beforeApplyChanges = function (screen) {
        // Write code here.
        if (screen.Property1 == 0) {
            //this line optional to show validation results immediately
            screen.findContentItem("Property1")._alwaysShowValidationResults = true;
    
            //add validation results
            screen.findContentItem("Property1").validationResults = [
            new msls.ValidationResult(
            screen.details.properties.Property1,
            "Property1 cannot be '0'.")
            ];
            return false;
        }
    };

    HTH,

    Josh

    Wednesday, October 1, 2014 6:40 PM
  •  

    Thanks for the replay. It would be good suggestion if the error could go away more dynamically. The problem is that if users change the value of screen.Property1 via a UI control, the red error box and message still on the UI which is not consistent with some of built in controls. Is there anyway to remove the error red box and messages as soon as users type or select a valid value. In my test, the screen.Property1 is an integer with either textbox and dropdown controls. Also there is one more problem with this approach, with red box and error message on the UI, users will not be able to save the changes (I am using a button which calls myapp.applyChanges method) and the beforeApplyChanges will never be fired again.

    It seems that the beforeApplyChanges is fired only when users clicks save button. I am thinking using databind to remove the msls.ValidationResults by an empty error. It seems working but this might require me to redesign how the validation will be implemented.

    Regards. 



    jl




    • Edited by Joe Lee Thursday, October 2, 2014 1:22 AM
    Thursday, October 2, 2014 12:58 AM
  • Joe,

    You're correct, it appears beforeApplyChanges doesn't fire again while there are validation results on the screen.  That's weird, you'd think it would be a good place to clear validation results.

    Anyway, you can run validation as the user types by doing so in the input keyup handler like so:

    myapp.AddEditOrder.Property1_postRender = function (element, contentItem) {
    
        inputElement = $(element).find("input")[0];
        $(inputElement).keyup(function() {
            if (inputElement.value == 0) {
                //this line to show validation results immediately
                contentItem._alwaysShowValidationResults = true;
    
                //add validation results
                contentItem.validationResults = [
                    new msls.ValidationResult(
                        contentItem.screen.details.properties.Property1,
                        "Property1 cannot be '0'.")
                ];
            } else {
                //remove validation results
                contentItem.validationResults = [];
            }
        });
    };

    HTH,

    Josh


    • Edited by joshbooker Friday, October 3, 2014 1:54 PM
    • Marked as answer by Angie Xu Wednesday, October 8, 2014 1:59 AM
    Friday, October 3, 2014 1:53 PM