web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Verify if an entity form is valid using javascript

(0) ShareShare
ReportReport
Posted on by

Hi,

    on my opportunity entity, I added a business rule that switches fields from required to not required following certain conditions. Now I would like to know if there's a javascript method from the SDK I could call to know if my current form is valid (if all my required fields have a value).

Thanks for your help

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Frantz,

    What are you trying to do? I would expect the required fields are all validated when you try to hit the Save button. Is that not happening?

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    No, I want to do more complex thing. I was only wondering and hoping a simple feature as validating a form in javascript exists... But from your answer, asking what I want to do, I understand there is no simple way in javascript (a simple method) to know if current entity form is valid...

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    You can add your own custom validation to the save event and cancel the save if it fails (Check out this link) . I don't think there's an easy way to see if all the required fields have a value though, because it won't hit your custom validation until all the required fields have a value.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

      Aiden

  • Verified answer
    necsa Profile Picture
    3,455 on at

    Hi Frantz,

    you can check your mandatory fields are populated or not as following,

    function IsFormValidForSaving(){
    var valid = true;

    Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
       if (attribute.getRequiredLevel() == "required") {
          if(attribute.getValue() == null){
              if(valid){
                  var control = attribute.controls.get(0);
                  alert(control.getLabel() + " is missing a value");
                  control.setFocus();
              }
              valid = false;

          }
       }
    });

    return valid;

    }

  • Community Member Profile Picture
    on at

    Hi All ,

     I used Necdet Saritas's code in CRM 2015 update 1 but its only validating business required attributes only.

    In my scenario  on account entity 5 fields are business required and for other 3 fields  i am setting required level conditionally using javascript on CRM form load.

    function IsFormValidForSaving() {
    var valid = true;
    Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
    if (attribute.getRequiredLevel() == "required" ) {
    if (attribute.getValue() == null) {
    if (valid) {
    var control = attribute.controls.get(0);
    control.setFocus();
    //console.log(control.getLabel() + " is missing a value");

    }
    valid = false;
    }
    }

    });

    return valid;

    }

    This code is not validation custom required attributes ( i am getting required level none for conditional custom required attributes).

    Can any one help for this issue?

    Regards,

    Jaydip Patil

     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans