Skip to Content

Define a Validation Rule in an MDK App

Write a JavaScript logic to validate an email address format in an MDK app.
You will learn
  • How to write a rule to validate an UI field
jitendrakansalJitendra KansalJuly 8, 2022
Created by
jitendrakansal
April 23, 2019
Contributors
jitendrakansal
jitendrakansal
jitendrakansal
MDK
  • Step 1

    When allowing end-users to make updates to data, it is important to add validation rules to verify that they are entering valid information.
    If the Update action fails due to the validation rule, the application will display a validation failure message to the end-user. You can customize this message by creating a Message action and adding the action to the validation Failure Message field in the Update action.

    1. Right-click the Actions folder | MDK: New Action | choose MDK Message Actions in Category | click Message Action | Next.

      MDK
    2. Provide the below information:

      Field Value
      Action NameValidationFailure
      Type Select Message from the dropdown
      MessageEmail address is not in the correct format recipient @ domain . domaintype
      Title Validate Email
      OKCaptionOK
      OnOK --None--
      CancelCaption leave it blank
      OnCancel --None--
      MDK
    3. Click Next and then Finish on the Confirmation step.

  • Step 2

    In the MDK editor, you will create a new JavaScript file called EmailValidation.js to handle validation if a @ symbol exists in the email address. In the validation rule, you will grab the data entered by the end-user, validate it and check for the @ symbol then return true if the email address is of a valid format or false if it is not. The returning result of the validation rule can be used in the Update action to determine whether the action succeeds or fails.

    You can find more details about writing a Rule.

    1. Right-click the Rules folder | MDK: New Rule File | select Empty JS Rule.

      MDK
    2. Enter the Rule name EmailValidation, click Next and then Finish on the confirmation step.

      MDK
    3. Replace the generated snippet with below code.

      JavaScript
      Copy
      export default function EmailValidation(context) {
          //The following evaluateTargetPath will retrieve the current value of the email control
          if ((context.evaluateTargetPath('#Control:FCEmail/#Value').indexOf('@')) === -1) {
              //If email value does not contain @ display a validation failure message to the end-user
              context.executeAction('/DemoSampleApp/Actions/ValidationFailure.action');
          } else {
              //If @ is present in the email value, return true to indicate validation is successful
              return true;
          }
      }
      
    4. Save the changes.

      The indexOf() method returns the index within the calling String object of the first occurrence of the specified value and -1, if no occurrence is found.

      In above code there is a reference to ValidationFailure.action , you can navigate directly from here to the MDK file by right clicking on it.

      MDK

  • Step 3

    Now, that you have created the validation rule, you will add it to the Update action. This will tell the Update action to run the validation rule before saving any data. If the validation rule is successful, the Update action will save the changes as expected. If the validation rule fails, the end-user receives the validation failure message telling them useful information so they can fix the problem before continuing.

    1. Open Customers_UpdateEntity.action by double clicking on the action in the project explorer pane.

    2. Expand the Common Action Properties and select EmailValidation.js rule for Validation Rule field.

      MDK

    What is the executeAction path set in EmailValidation.js rule?

  • Step 4

    Deploy the updated application to your MDK client.

    1. Right-click Application.app and select MDK: Deploy.

      MDK
    2. Select deploy target as Mobile & Cloud.

      MDK

      You should see success message for both deployments.

      MDK

      Alternatively, you can select MDK: Redeploy in the command palette (View menu>Find Command OR press Command+Shift+p on Mac OR press Ctrl+Shift+P on Windows machine), it will perform the last deployment.

      MDK
  • Step 5

    Make sure you are choosing the right device platform tab above.

    Once you complete this tutorial you can continue with enhance Your First MDK App with Additional Functionalities mission.

    For which example, validation rule returns false?

Back to top