Skip to Content

Define a Validation Rule in an MDK App

test
0 %
Define a Validation Rule in an MDK App
Details
// Explore More Tutorials

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


Step 1: Create a validation message

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.

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

MDK

Provide the below information:

Field Value
Action Name ValidationFailure
Type Message
Message Email address is not in the correct format recipient @ domain . domaintype
Title Validate Email
OKCaption OK
OnOK --None--
CancelCaption leave it blank
OnCancel --None--
MDK

Click Next and then Finish on the Confirmation step.

Log on to answer question
Step 2: Create a new rule

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.

Right click on the Rules folder | New | File.

MDK

Enter the file name EmailValidation.js, click OK.

Copy and paste the following code.

export default function EmailValidation(atSign) {

//The following evaluateTargetPath will retrieve the current value of the email control
if((atSign.evaluateTargetPath('#Control:FCEmail/#Value').indexOf('@')) === -1){

//If email value does not contain @ display a validation failure message to the end-user
  atSign.executeAction('/DemoSampleApp/Actions/ValidationFailure.action');
  } else {

//If @ is present in the email value, return true to indicate validation is successful
    return true;
  }
}

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

Log on to answer question
Step 3: Add the validtion rule to the Update entity

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.

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

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

MDK
Log on to answer question
Step 4: Deploy and activate the application

Deploy the updated application to your MDK client.

Right-click on the DemoSampleApp MDK Application in the project explorer pane and select MDK Deploy and Activate.

MDK

Since we have deployed already both the destination and app id should be pre-selected based on the last time you deployed our application.  Confirm the Destination Name is mobileservices_cf and the Application Id is com.sap.mdk.demo and click Next.

MDK
Log on to answer question
Step 5: Test the application

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

Re-launch the app on your device, you may asked to authenticate with passcode or Fingerprint. You will see a Confirmation pop-up, click OK.

Update a record with no contain of @ in Email property throws a validation failure message.

MDKMDK

Re-launch the app on your device, you may asked to authenticate with passcode or Touch ID. You will see a Confirmation pop-up, click OK.

Update a record with no contain of @ in Email property throws a validation failure message.

MDKMDK
For which example, validation rule returns false?
×

Next Steps

Back to top