Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Validations in Portal page- My code not working properly

Posted on by 465

Hi All,

I am using PowerApps portals in D365.

I have two radio buttons on the page(Yes and No) and my all controls are in section and I am hiding section when i select "No" radio button and showing when i select "Yes"

I want validations when select Yes radio button and don't want any validations if I select No radio button

I have implemented following(changed id's to not disclose source code) code. I am getting following problems

1. It is raising validations even I select "No" radio button and submit the form

2. It is adding duplicate validation error messages(It is happening when I keep on clicking on Yes and No radio buttons) and I submit the form

Please help me. It is bit urgent for me.

Here is my code : 

$(document).ready(function() {

if (($('#Radio_Yes').is(':checked')) && (!$('#Radio_No').is(':checked')))
{
addValidator('firstname', "Firstname");
addValidator('lastname', "Lastname");
}


function addValidator(fieldName, fieldLabel) {

if (typeof (Page_Validators) == 'undefined') return;
$("#" + fieldName + "_label").parent().addClass("required");
var newValidator = document.createElement('span');
newValidator.style.display = "none";
newValidator.controltovalidate = fieldName;
newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + " is a required field.</a>";
newValidator.validationGroup = "";
newValidator.initialvalue = "";
newValidator.evaluationfunction = function () {
var value = $("#" + fieldName).val();
if (value === null || value === "") {
return false;
} else {
return true;
}
};

function removeValidator(fieldName)
{
$.each(Page_Validators, function (index, validator) {
if (validator.id == "RequiredFieldValidator" + fieldName) {
Page_Validators.splice(index, 1);
}
});
$("#" + fieldName + "_label").parent().removeClass("required");

}

// Add the new validator to the page validators array:
if ($('#Radio_Yes').is(':checked'))
{
Page_Validators.push(newValidator);
}

$("#Radio_No").change(function() {
if (this.checked)
{

removeValidator("firstname");
removeValidator("lastname");
$("[data-name=Contact_Details]").parent().hide();
}
});

$("#Radio_Yes").change(function() {
if (this.checked)
{

addValidator('firstname', "Firstname");
addValidator('lastname', "Lastname");
$("[data-name=Contact_Details]").parent().show();

}
});

// Wire-up the click event handler of the validation summary link

$("a[href='#" + fieldName + "_label']").on("click", function () {
scrollToAndFocus(fieldName + '_label', fieldName);

});
}

});

Thanks in Advance

Babu

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Validations in Portal page- My code not working properly

    Hi Babu,

    It seems that there are several lines you should change to code:

    1. When you iterate Page_Validators array in removeValidator, 

    if (validator.id == "RequiredFieldValidator" fieldName)

    you should make id property of newValidator object also has same format, e.g:

    newValidator.id = "RequiredFieldValidator"  fieldname;

    2. There is some issue when I was debugging forEach function: cannot read undefined property id...

    I'm not so familiar about the function so I changed it like below and it worked then:

    for (var i = 0;i < Page_Validators.length; i  ) {
        if (Page_Validators[i].id == "RequiredFieldValidator"   fieldname) {
          Page_Validators.splice(i, 1);
        }
    }
    

    3. In newValidator.evaluationfunction snippet, we should still check fields value manually or statically, 

    because scope of the "evaluationfunction" is whole form.

    newValidator.evaluationfunction = function() {   
        if ($("#lastname").val() == "" || $("#firstname").val() == "") {
          return false;
        } else {
          return true;
        }       
    };

    My test code:

    $(document).ready(function() {
    
        function addValidator(fieldname, fieldLabel) {
          // Add event validator
          if (typeof(Page_Validators) == 'undefined') return;
        // Create new validator
          var newValidator = document.createElement('span');
          newValidator.style.display = "none";
          newValidator.id = fieldname   "Validator";
          newValidator.controltovalidate = fieldname;
          newValidator.errormessage = ""   fieldLabel   " is a required field.";
          // value = $("#"   fieldname).val();
          newValidator.evaluationfunction = function() {   
            debugger;            
            if ($("#emailaddress1").val() == "" || $("#firstname").val() == "") {
              return false;
            } else {
              return true;
            }       
          };
          Page_Validators.push(newValidator);
        }
    
        function removeValidator(fieldname) {
          debugger;
          for (var i = 0;i < Page_Validators.length; i  ) {
            if (Page_Validators[i].id == fieldname   "Validator") {
              Page_Validators.splice(i, 1);
            }
          }
          // $.each(Page_Validators, function (index, validator) {
          //   if (typeof validator.id !== "undefined" && validator.id == fieldname   "Validator") {
          //     Page_Validators.splice(index, 1);
          //   }
          // });
        }
    
        // Add validator element
        $("#new_acceptance_0").change(function() {
            if (this.checked) {
                removeValidator("emailaddress1");
                removeValidator("firstname");
                $("#emailaddress1").attr('aria-required', "false");
                $('#emailaddress1').parent().prev().removeClass("required");
                $("#firstname").attr('aria-required', "false");
                $('#firstname').parent().prev().removeClass("required");
            }
        });
    
        $("#new_acceptance_1").change(function() {
            if (this.checked) {
                addValidator("emailaddress1", "Email");
                addValidator("firstname", "First Name");
                $("#emailaddress1").attr('aria-required', "true");
                $('#emailaddress1').parent().prev().addClass("required");
                $("#firstname").attr('aria-required', "true");
                $('#firstname').parent().prev().addClass("required");
            }
        });
    
    })

    Form:

    pastedimage1586756028784v1.png

    Regards,

    Clofly

  • Narendra Sunkara Profile Picture
    Narendra Sunkara 465 on at
    RE: Validations in Portal page- My code not working properly

    Hi Justin,

    Thanks for your response.

    I will try with suggested changes

    Regards,

    Babu

  • Suggested answer
    Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Validations in Portal page- My code not working properly

    Hi Babu,

    Missing "newValidator.id" and  you are trying to remove the "newValidator" object from Page_Validators by using id in "removeValidator" function. Please make following changes.

    newValidator.id = fieldName   "Validator";
    
    
    
    function removeValidator(fieldName) {
        Page_Validators = $.grep(Page_Validators, function (item) {
            return item.id !== fieldName   "Validator";
        });
    
        $("#"   fieldName   "_label").parent().removeClass("required");
    
    }
    
    
    // Add the new validator to the page validators array:
    if ($('#Radio_Yes').is(':checked') && Page_Validators.filter(e => e.id === fieldName   'Validator').length === 0) {
        Page_Validators.push(newValidator);
    }

    Thanks

    Justin Jose

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans