How To: Add Validation to a SmartForm

Validation patterns are regular expressions which can be configured on certain controls in the View Designer such as the Text Box control to enforce validation when data is captured at runtime.

These validation patterns will however not be executed unless this condition has been added to a rule in the View Designer. The condition needs to be configured in order to specify the controls on which validation will be executed. The validation will only be executed at runtime when this rule is called.

To better explain this we have created an example where a validation is added to a control to only accept positive decimals.

In order to achieve what is required above, we have followed the next steps:

In the SmartObject Designer do the following:

  1. Create a new SmartObject
  2. Provide a Name for the SmartObject, select which category to store it under and the type of SmartObject and click Next
  3. Add a new property of type Decimal
  4. Click Finish to save the SmartObject
  5. Click Generate a View on the Finish screen of the SmartObject Designer
  6. Generate an Item View using the SmartObject
  7. Edit the above created View

In the View Designer do the following:

  1. Navigate to the Layout step
  2. Select the [Textbox] control that was generated for the [Decimal] property
  3. Click on the ellipsis button, on the Pattern property
  4. Add a new Pattern as follows:

    Name: Positive Decimals

    Pattern: (^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)

    Message: The entered value was not a positive decimal value.

  5. Save the Pattern by clicking OK and apply it to the [Textbox] by selecting it and clicking OK again
  6. Click Finish to save the View 

Run the View and execute the CREATE method against a value that is expected to fail (a negative decimal value). Note that nothing is preventing the CREATE from being executed, but the message is displayed when hovering over the control. This is the expected behavior as we still have to configure the condition on the rule of the View.

Edit the above created View and navigate to the Rule step. On the Rule step do the following:

  1. Select the default CREATE method Rule, and select Edit Rule
  2. Continue to the Conditions step
  3. Add the The View passes validation Condition

    Please note that the execute CREATE method action should be part of the validation IF statement. If it's two statements the design time validation will not pass and will prevent you from completing the Rules Wizard. In order to make it one statement, move the action so it forms part of the IF statement and then delete the remaining action if necessary.

  4. Click on the (configure) link of the Condition
  5. Check the validate checkbox of the [Textbox] control and Show validation messages in a popup dialog checkbox at the bottom of the screen
  6. Save the configuration by clicking OK
  7. Save the Rule by clicking OK
  8. Click Finish to save the View

Now run the View again and use incorrect data. Note that the validation is applied, a message is displayed to inform the user of the validation that failed and the CREATE method is not executed.