Create Rules¶
Rules can do many things: You can retrieve images from a database, perform math, edit text, perform logic, create lists, create variables or implement functions. Rules implement business logic for the execution and managing of data.
Use the visual rules editor within your application to create business logic for your application. The editor uses the Google Blockly
visual code editor, which is easy to understand when creating native script code. See Google Blockly
Demos to familiarize yourself with Google Blockly
.
You must have a valid Mobile Development Kit project with existing pages.
The following steps describe how to create a rule visually within your Mobile Development Kit application:
- Right-click the
Rules
folder and selectMDK: New Rule
- Provide a rule name and select
Finish
to complete. - The
Rule Editor
displays.
For complex rule logic, it is recommended to write rules directly in JavaScript.
Rule Editor¶
The Rule Editor allows you to create a visual representation of your business logic which generates JavaScript code.
Use the blocks to create a rule that enforces business logic (do something), create a navigation path, compute field values based on the following categories:
- Logic – if, Boolean
- Loops – do something for a certain count
- Math – simple to advanced math
- Lists – create list, get a list item
- Variables – create and alter variables
- Functions – create functions for reusability
- Text – simple to advanced text manipulation
- Library – contains randomize block
- System – manipulation of data and time
- Client APIs – can query application state and data
- Predefined – where predefined rules are stored
See Customizing Advanced Rules for more details.
When saving your rule, there are two files associated with each rule. One has the extension .rule
and the other has a rule.js
extension. The .rule
file is the visual representation of the code while the rule.js
contains the JavaScript code. If you edit the rule.js
file, the visual representation, .rule
content does not change. However, if you change your rule within the .rule
file, you overwrite the rule.js
file.
Create a Simple Rule¶
This validation rule determines if an editable field is blank when you try to save it. It returns True or False depending on the content of the field. This example displays a message to the user so that it can be fixed. Message actions are not always required.
Context¶
You have opened the MDK Rule Editor
from Create Rules.
Procedure¶
-
Select the
Functions
from the left column of the rule editor and select theif [puzzle piece] return [puzzle piece]
. Drag this piece into the editor. -
Fill in the first empty puzzle piece to determine what you are comparing:
- Navigate to the
Client APIs
menu on the left side of the editor. Use the puzzle piece that saysclientAPI.binding
and drag it into the first empty puzzle piece. - There is a pull-down menu next to the word
binding
, selectevaluateTargetPath
. Tell the target path where to go by selecting theObject Browser
fromClient APIs
and navigate to the field to verify if it is blank. - On the
Object Browser
page, changeOData Collections
toUI Properties
and search for the field that is being validated on the edit page. - Double-click the field so that the expression is entered in the box on the bottom of the page and select
OK
.
- Navigate to the
-
The
evaluateTargetPath
returnsTrue
if there is content in that field orFalse
if it is blank. Go to theBoolean
section and drag theTrue
puzzle piece on the same line as theevaluateTargetPath
andFalse
on the next line. -
Warn the user that the field is empty by creating a message action. Right-click on the
Actions
folder and selectMDK: New Action
. -
On the
Template Section
page, chooseMessage Action
and selectNext
. -
On the new page, provide a name for the action. The type is
Message
because you must do something. In this case, go back and fix the problem so the entity can be saved. Provide a friendly message and a title, and OK in theOKCaption
field. SelectFinish
to complete. -
Add the rule and the warning message to the affected page. Find the Update action for the page we edited under the
Actions
folder. This page contains the action that saves the data, select that action. -
Navigate to
Common Action Properties
and findValidation Rule
. Select theObject Browser
icon on the far right and select the rule that was recently created. SelectOK
. -
Find the failure action that is recently created using the object browser for
Validation Failure Action
. SelectOK
to finish. -
Don't forget to save your project and test your rule!
For advanced usage of rules, see Customizing with Advanced Rules
For use of built-in NativeScript modules, see NativeScript Framework Modules