Define Rules
- Last UpdatedNov 15, 2023
- 1 minute read
Define rules for a custom control to validate the data using the client-side JavaScript.
To define custom control rules
-
Override the Rules collection property in the custom control class.
-
Define the rule name, rule definition, control type, and render mode, if required.
-
At the client-side custom control JavaScript file, implement a JavaScript function with the following in mind:
-
Use the same name for the JavaScript function as that of the rule name defined in the custom control class.
-
Use the Control, ControlValue and ruleObject for parameters.
-
Verify the ControlValue parameter before taking any action.
-
Get the rule definition message using ruleObject.ErrorMessage property.
Example: Custom Control Class
/// <summary>
/// Rules for the control
/// </summary>
public override Collection<Rule> Rules
{
get
{
Collection<Rule> rules = base.Rules;
string ruleName = "MinMaxNumberValidation";
string errorMessage = "$Name$ value is out of range.";
rules.Insert(0, new Rule { RuleFor = "Value", RuleName = ruleName, RuleDefinition = errorMessage,
ControlType = this.GetType().Name, RenderMode = this.RenderAs.ToString() });
return rules;
}
}
Example: Custom Control Client-side JavaScript
function minMaxNumberValidation(controlValue, control, ruleObject)
{
if (control.renderMode === "NonHtml5")
{
var errorMessage = "";
var errorBoolValue = false;
var errorCode = errorConstants.none;
var value = parseFloat(controlValue);
if (!isNaN(value))
{
if ((value >= control.min && value <= control.max) == false)
{
errorBoolValue = true;
errorCode = errorConstants.valueValidationRules;
control.validationError(errorBoolValue);
control.validationErrorMessage(Skelta.localize.getString(ruleObject.errorMessage).replace("$Name$", control.xmlNodeBoundTo.toString()));
control.validationErrorCode(errorCode);
}
}}
}