locked
Focus back to error field in beforeApplyChanges RRS feed

  • Question

  • What's the best way to set the focus to the error text box in beforeApplyChanges? I must be missing something simple. It's more convenient for the user to return focus to the error.

    myapp.OPP_AddEditOpportunity.beforeApplyChanges = function (screen) {
        // validate opp title
        if (typeof screen.OPPORTUNITY.Title == "undefined" || screen.OPPORTUNITY.Title.trim() == "") {
            screen.findContentItem("Title")._alwaysShowValidationResults = true;
            screen.findContentItem("Title").validationResults = [
            new msls.ValidationResult(
                screen.OPPORTUNITY.details.properties.Title,
                "Opportunity title can not be blank.")
            ];
            // none of these worked...
            //var contentItem = screen.findContentItem("Title")._view._container[0];
            //contentItem.focus();
            //$('input[name=Title]').focus();
            return false;
        }
    };

    Thanks 

    Sunday, November 1, 2015 3:30 PM

Answers

  • Hi dcagamer,

    I have tested your code in my side and I find that the input doesn't have name attribute on screen, it just has id. The id is generated as below image, it is not "Title". You can watch it with Developer Tools(F12) in IE.

    So we can't operate this input with its name. I define a global variable on the page and then set value to it when the Title input post_Render. Then we can operate it in beforeApplyChanges method. Please try with following code, which have been tested in my side.

    var $TitleInput;
    myapp.AddEditArticle.Title_postRender = function (element, contentItem) {
        $TitleInput= $("input", $(element));
    
    };
    
    myapp.AddEditArticle.beforeApplyChanges = function (screen) {
        if (typeof screen.Article.Title == "undefined" || screen.Article.Title.trim() == "") {
            screen.findContentItem("Title")._alwaysShowValidationResults = true;
            screen.findContentItem("Title").validationResults = [
                new msls.ValidationResult(screen.Article.details.properties.Title,
                    "Article title can not be blank")
            ];
    
            $TitleInput.focus();
            return false;
        }
    };

    Best Regards,
    Weiwei

    • Proposed as answer by Angie Xu Tuesday, November 17, 2015 2:01 AM
    • Marked as answer by Angie Xu Tuesday, November 17, 2015 2:01 AM
    Wednesday, November 4, 2015 9:08 AM
    Moderator

All replies

  • Hi dcagamer,

    I have tested your code in my side and I find that the input doesn't have name attribute on screen, it just has id. The id is generated as below image, it is not "Title". You can watch it with Developer Tools(F12) in IE.

    So we can't operate this input with its name. I define a global variable on the page and then set value to it when the Title input post_Render. Then we can operate it in beforeApplyChanges method. Please try with following code, which have been tested in my side.

    var $TitleInput;
    myapp.AddEditArticle.Title_postRender = function (element, contentItem) {
        $TitleInput= $("input", $(element));
    
    };
    
    myapp.AddEditArticle.beforeApplyChanges = function (screen) {
        if (typeof screen.Article.Title == "undefined" || screen.Article.Title.trim() == "") {
            screen.findContentItem("Title")._alwaysShowValidationResults = true;
            screen.findContentItem("Title").validationResults = [
                new msls.ValidationResult(screen.Article.details.properties.Title,
                    "Article title can not be blank")
            ];
    
            $TitleInput.focus();
            return false;
        }
    };

    Best Regards,
    Weiwei

    • Proposed as answer by Angie Xu Tuesday, November 17, 2015 2:01 AM
    • Marked as answer by Angie Xu Tuesday, November 17, 2015 2:01 AM
    Wednesday, November 4, 2015 9:08 AM
    Moderator
  • You could do the validation in the postRender method and avoid the ghastly global variable.
    Thursday, November 5, 2015 5:01 AM
  • Thanks all - this helps me get what I needed.
    Wednesday, November 18, 2015 1:12 AM