Skip to content

Latest commit

 

History

History
106 lines (84 loc) · 3.87 KB

validation.md

File metadata and controls

106 lines (84 loc) · 3.87 KB
title page_title description slug tags published position
Data Validation
Data Validation
The article describes the data validation feature of RadCardView.
radcardview-features-validation
editing,cardview
true
8

Data Validation

The data fields of the RadCardView control support data validation of the user input.

The feature provides visual feedback when an invalid value is entered. This article shows how to implement custom validation logic.

Using Validation Events

The RadCardView control provides few events that are raised whenever the data is changed using the UI - CardValidating, CardDataFieldValidating and CardDataFieldValidated.

The CardDataFieldValidating and CardDataFieldValidated events are raised when a data field exits its edit mode. If the card loses the focus when a data field exits edit mode, then the CardValidating event is raised.

CardDataFieldValidating can be used to validate the associated value and determine the validation state of the data field.

[C#] Example 1: Validating a data field

{{region radcardview-features-validation-0}} private void RadCardView_CardDataFieldValidating(object sender, CardDataFieldValidatingEventArgs e) { if (e.DataField.DataMemberBinding.Path.Path == "Number") { var dataItem = (CardInfo)e.DataField.Item; if (dataItem.Number < 0) { e.IsValid = false; } } } {{endregion}}

Using ValidationException

The validation can be implemented in the view model. If the updated value is invalid, a ValidationException can be thrown in the corresponding property setter. To enable the visual feedback in the UI, set the ValidatesOnExceptions and NotifyOnValidationError properties of the DataMemberBinding to True.

[C#] Example 2: Implementing validation logic in the view model

{{region radcardview-features-validation-1}} private int number; public int Number { get { return number; } set { if (value < 0) { throw new ValidationException("Positive number required."); } else { number = value; } } } {{endregion}}

[XAML] Example 3: Enabling visual feedback using the data binding properties

{{region radcardview-features-validation-2}} telerik:RadCardView.DataFieldDescriptors
<telerik:CardDataFieldDescriptor DataMemberBinding="{Binding Number, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> </telerik:RadCardView.DataFieldDescriptors> {{endregion}}

Figure 1: Data validation visual feedback

{{ site.framework_name }} RadCardView Data validation visual feedback

Using Data Annotations

The data binding validation can be enabled also by using the data annotation attributes.

[C#] Example 4: Using data annotations in the view model

{{region radcardview-features-validation-3}} private int number;

[Range(0, int.MaxValue, ErrorMessage = "Positive number required.")]
public int Number
{
	get { return number; }
	set { number = value; } }
}

{{endregion}}

[XAML] Example 5: Data binding the view model property

{{region radcardview-features-validation-4}} telerik:RadCardView.DataFieldDescriptors
<telerik:CardDataFieldDescriptor DataMemberBinding="{Binding Number, Mode=TwoWay}" /> </telerik:RadCardView.DataFieldDescriptors> {{endregion}}

In order to see the error visual when using data annotations, press the Enter key. This will add the error in the list at the bottom of the card.

Figure 2: Data validation visual feedback when using data annotations

{{ site.framework_name }} RadCardView Data validation visual feedback when using data annotations

See Also

  • [Getting Started]({%slug radcardview-getting-started%})
  • [Events]({%slug radcardview-events%})
  • [Visual Structure]({%slug radcardview-visual-structure%})