Validation Rules

RadValidationProvider supports three types of validation rules all of which implements the IRadValidationRule interface:

  • RadValidationRule: allows you to define validation logic for a single control considering a validation operator and a value.
  • RadValidationRuleWithTargetControl: allows you to define validation logic of a control against a second control, e.g. if you have two RadDateTimePicker controls for selecting start/end date and you need to ensure that start date is less than end date.
  • RadCompositeValidationRule: allows defining more complex validation logic by adding multiple simpler RadValidationRules.

RadValidationRule

RadValidationRule requires the following properties to be set in order to define a validation logic for a single control:

  • AutoToolTip: a boolean value indicating whether a tool tip will be shown when the validation fails.
  • Controls: a collection that contains all controls to which this rule is applied, e.g. you can use one rule for multiple controls.
  • Operator: specifies the operator to be used for the validation logic. It is of type FilterOperator. Hence, similar to the filter operator, you can use the same validation operators.
  • Value: specifies the value for the validation comparison.
  • ToolTipText: specifies the text to be displayed when the validation fails.
  • ToolTipTitle: specifies the title of the error tool tip. Its default value is "Validation Failed".

The following code snippet demonstrates how to add a validation rule ensuring that a RadTextBox is not empty. When you focus the control and try to exit without entering any text, the validation logic won't succeed and the error tool tip will be displayed:

validation-provider-validation-rules 001

RadValidationRule radValidationRule1 = new RadValidationRule();
radValidationRule1.AutoToolTip = true;
radValidationRule1.AddControl(this.radTextBox1);
radValidationRule1.Operator = Telerik.WinControls.Data.FilterOperator.IsNotLike;
radValidationRule1.PropertyName = "Text";
radValidationRule1.ToolTipText = "Location can\'t be empty!";
radValidationRule1.Value = "";

radValidationProvider1.ValidationRules.Add(radValidationRule1);

Dim radValidationRule1 As RadValidationRule = New RadValidationRule()
radValidationRule1.AutoToolTip = True
radValidationRule1.AddControl(Me.RadTextBox1)
radValidationRule1.[Operator] = Telerik.WinControls.Data.FilterOperator.IsNotLike
radValidationRule1.PropertyName = "Text"
radValidationRule1.ToolTipText = "Location can't be empty!"
radValidationRule1.Value = ""
radValidationProvider1.ValidationRules.Add(radValidationRule1)

RadValidationRuleWithTargetControl

RadValidationRuleWithTargetControl requires the following properties to be set in order to define a validation logic for a control against a second control:

  • AutoToolTip: a boolean value indicating whether a tool tip will be shown when the validation fails.
  • Controls: a collection that contains all controls to which this rule is applied, e.g. you can use one rule for multiple controls.
  • Operator: specifies the operator to be used for the validation logic. It is of type FilterOperator. Hence, similar to the filter operator, you can use the same validation operators.
  • PropertyName: specifies the name of the property which to be used for the validation comparison.
  • TargetControl: specifies the second control against which the validation logic will be performed.
  • TargetControlPropertyName: specifies the name of the second control's property which to be used for the validation comparison.
  • ToolTipText: specifies the text to be displayed when the validation fails.
  • ToolTipTitle: specifies the title of the error tool tip. Its default value is "Validation Failed".

The following rule will ensure that the first RadDateTimePicker's value is less than the value of the second one:

validation-provider-validation-rules 002

RadValidationRuleWithTargetControl radValidationRuleWithTargetControl1 = new RadValidationRuleWithTargetControl();
radValidationRuleWithTargetControl1.AutoToolTip = true;
radValidationRuleWithTargetControl1.AddControl(this.radDateTimePicker1);
radValidationRuleWithTargetControl1.Operator = Telerik.WinControls.Data.FilterOperator.IsLessThan;
radValidationRuleWithTargetControl1.PropertyName = "Value";
radValidationRuleWithTargetControl1.TargetControl = this.radDateTimePicker2;
radValidationRuleWithTargetControl1.TargetControlPropertyName = "Value";
radValidationRuleWithTargetControl1.ToolTipText = "Start date should be less than End date!";
radValidationProvider1.ValidationRules.Add(radValidationRuleWithTargetControl1);

Dim radValidationRuleWithTargetControl1 As RadValidationRuleWithTargetControl = New RadValidationRuleWithTargetControl()
radValidationRuleWithTargetControl1.AutoToolTip = True
radValidationRuleWithTargetControl1.AddControl(Me.RadDateTimePicker1)
radValidationRuleWithTargetControl1.[Operator] = Telerik.WinControls.Data.FilterOperator.IsLessThan
radValidationRuleWithTargetControl1.PropertyName = "Value"
radValidationRuleWithTargetControl1.TargetControl = Me.RadDateTimePicker2
radValidationRuleWithTargetControl1.TargetControlPropertyName = "Value"
radValidationRuleWithTargetControl1.ToolTipText = "Start date should be less than End date!"
radValidationProvider1.ValidationRules.Add(radValidationRuleWithTargetControl1)

In order to cover the case that the second RadDateTimePicker is greater than the first one, you will need to add one more RadValidationRuleWithTargetControl:

validation-provider-validation-rules 003

RadValidationRuleWithTargetControl radValidationRuleWithTargetControl2 = new RadValidationRuleWithTargetControl();
radValidationRuleWithTargetControl2.AutoToolTip = true;
radValidationRuleWithTargetControl2.AddControl(this.radDateTimePicker2);
radValidationRuleWithTargetControl2.Operator = Telerik.WinControls.Data.FilterOperator.IsGreaterThan;
radValidationRuleWithTargetControl2.PropertyName = "Value";
radValidationRuleWithTargetControl2.TargetControl = this.radDateTimePicker1;
radValidationRuleWithTargetControl2.TargetControlPropertyName = "Value";
radValidationRuleWithTargetControl2.ToolTipText = "End date should be greater than Start date!";
radValidationProvider1.ValidationRules.Add(radValidationRuleWithTargetControl2);

Dim radValidationRuleWithTargetControl2 As RadValidationRuleWithTargetControl = New RadValidationRuleWithTargetControl()
radValidationRuleWithTargetControl2.AutoToolTip = True
radValidationRuleWithTargetControl2.AddControl(Me.RadDateTimePicker2)
radValidationRuleWithTargetControl2.[Operator] = Telerik.WinControls.Data.FilterOperator.IsGreaterThan
radValidationRuleWithTargetControl2.PropertyName = "Value"
radValidationRuleWithTargetControl2.TargetControl = Me.RadDateTimePicker1
radValidationRuleWithTargetControl2.TargetControlPropertyName = "Value"
radValidationRuleWithTargetControl2.ToolTipText = "End date should be greater than Start date!"
radValidationProvider1.ValidationRules.Add(radValidationRuleWithTargetControl2)

RadCompositeValidationRule

RadCompositeValidationRule allows you to define a collection of multiple validation rules added to the ValidationRules collection.

The following code snippet demonstrates how to define a RadCompositeValidationRule composed of two RadValidationRuleWithTargetControls and one RadValidationRule ensuring that Car Price (radSpinEditor1) is greater than Minimum Price (radSpinEditor2) and less than Maximum Price (radSpinEditor3). In addition, the price cannot be equal to 5000:

None of the associated controls (e.g. radSpinEditor1) of a composite rule should be added as a target in any of the nested rules! Please be careful when defining composite rules in order to get the proper validation logic. Usually, it is even possible to simplify the validation logic and define separate RadValidationRuleWithTargetControls outside a composite rule.

this.radSpinEditor2.Minimum = 0;
this.radSpinEditor2.Maximum = 10000;
this.radSpinEditor2.Value = 1000;
this.radSpinEditor3.Minimum = 0;
this.radSpinEditor3.Maximum = 10000;
this.radSpinEditor3.Value = 10000;
this.radSpinEditor1.Maximum = 10000;
RadCompositeValidationRule compositeValidationRule = new RadCompositeValidationRule();
compositeValidationRule.LogicalOperator = Telerik.WinControls.Data.FilterLogicalOperator.And;
compositeValidationRule.ToolTipText = "Car price should be greater than Minimum and less than Maximum price!";
RadValidationRuleWithTargetControl radValidationRuleWithTargetControl1 = new RadValidationRuleWithTargetControl();
radValidationRuleWithTargetControl1.AutoToolTip = true;
radValidationRuleWithTargetControl1.PropertyName = "Value";
radValidationRuleWithTargetControl1.AddControl(this.radSpinEditor1);
radValidationRuleWithTargetControl1.Operator = Telerik.WinControls.Data.FilterOperator.IsGreaterThan;
radValidationRuleWithTargetControl1.TargetControl = this.radSpinEditor2;
radValidationRuleWithTargetControl1.TargetControlPropertyName = "Value";
compositeValidationRule.ValidationRules.Add(radValidationRuleWithTargetControl1);
RadValidationRuleWithTargetControl radValidationRuleWithTargetControl2 = new RadValidationRuleWithTargetControl();
radValidationRuleWithTargetControl2.AutoToolTip = true;
radValidationRuleWithTargetControl2.PropertyName = "Value";
radValidationRuleWithTargetControl2.AddControl(this.radSpinEditor1);
radValidationRuleWithTargetControl2.Operator = Telerik.WinControls.Data.FilterOperator.IsLessThan;
radValidationRuleWithTargetControl2.TargetControl = this.radSpinEditor3;
radValidationRuleWithTargetControl2.TargetControlPropertyName = "Value";
compositeValidationRule.ValidationRules.Add(radValidationRuleWithTargetControl2);
RadValidationRule radValidationRule3 = new RadValidationRule();
radValidationRule3.AutoToolTip = true;
radValidationRule3.AddControl(this.radSpinEditor1);
radValidationRule3.PropertyName = "Value";
radValidationRule3.Operator = Telerik.WinControls.Data.FilterOperator.IsNotEqualTo;
radValidationRule3.Value = 5000;
compositeValidationRule.ValidationRules.Add(radValidationRule3);
this.radValidationProvider1.ValidationRules.Add(compositeValidationRule);

Me.RadSpinEditor2.Minimum = 0
Me.RadSpinEditor2.Maximum = 10000
Me.RadSpinEditor2.Value = 1000
Me.RadSpinEditor3.Minimum = 0
Me.RadSpinEditor3.Maximum = 10000
Me.RadSpinEditor3.Value = 10000
Me.RadSpinEditor1.Maximum = 10000
Dim compositeValidationRule As RadCompositeValidationRule = New RadCompositeValidationRule()
compositeValidationRule.LogicalOperator = Telerik.WinControls.Data.FilterLogicalOperator.[And]
compositeValidationRule.ToolTipText = "Car price should be greater than Minimum and less than Maximum price!"
Dim radValidationRuleWithTargetControl1 As RadValidationRuleWithTargetControl = New RadValidationRuleWithTargetControl()
radValidationRuleWithTargetControl1.AutoToolTip = True
radValidationRuleWithTargetControl1.PropertyName = "Value"
radValidationRuleWithTargetControl1.AddControl(Me.RadSpinEditor1)
radValidationRuleWithTargetControl1.[Operator] = Telerik.WinControls.Data.FilterOperator.IsGreaterThan
radValidationRuleWithTargetControl1.TargetControl = Me.RadSpinEditor2
radValidationRuleWithTargetControl1.TargetControlPropertyName = "Value"
compositeValidationRule.ValidationRules.Add(radValidationRuleWithTargetControl1)
Dim radValidationRuleWithTargetControl2 As RadValidationRuleWithTargetControl = New RadValidationRuleWithTargetControl()
radValidationRuleWithTargetControl2.AutoToolTip = True
radValidationRuleWithTargetControl2.PropertyName = "Value"
radValidationRuleWithTargetControl2.AddControl(Me.RadSpinEditor1)
radValidationRuleWithTargetControl2.[Operator] = Telerik.WinControls.Data.FilterOperator.IsLessThan
radValidationRuleWithTargetControl2.TargetControl = Me.RadSpinEditor3
radValidationRuleWithTargetControl2.TargetControlPropertyName = "Value"
compositeValidationRule.ValidationRules.Add(radValidationRuleWithTargetControl2)
Dim radValidationRule3 As RadValidationRule = New RadValidationRule()
radValidationRule3.AutoToolTip = True
radValidationRule3.AddControl(Me.RadSpinEditor1)
radValidationRule3.PropertyName = "Value"
radValidationRule3.[Operator] = Telerik.WinControls.Data.FilterOperator.IsNotEqualTo
radValidationRule3.Value = 5000
compositeValidationRule.ValidationRules.Add(radValidationRule3)
Me.radValidationProvider1.ValidationRules.Add(compositeValidationRule)

validation-provider-validation-rules 004

When the validation fails, the ControlValidation event is fired. The RadValidationEventArgs offers the following information:

  • ValidationHelperElement: stores the styling information of the error indication.
  • IsValid: determines whether the controls is validated or not. Even though the validation may fail according to the defined rules, you can determine that the situation is valid in certain cases and set it to true.
  • Control: returns the control being validated.
  • ErrorImage: specifies the error image that is being shown when the validation fails.
  • ErrorSvgImage: specifies the error vector image that is being shown when the validation fails.
  • ToolTip: gives you access to the error tooltip.
  • ToolTipX and ToolTipY: specifies the location of the tooltip.
  • ToolTipDuration: specifies the duration how long the tooltip is being shown.
  • ValidationRule: returns the rule that fails.
  • ErrorText: specifies the error message.
  • ErrorTitle: specifies the error title.
  • DisplayIconAndToolTip: controls whether to display icon and tooltip.
  • EnableToolTipShadow: controls whether the tooltip's shadow will be rendered.

See Also

In this article
Not finding the help you need? Improve this article