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 |
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.
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.
{{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}}
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.
{{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}}
{{region radcardview-features-validation-2}}
telerik:RadCardView.DataFieldDescriptors
<telerik:CardDataFieldDescriptor DataMemberBinding="{Binding Number, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
</telerik:RadCardView.DataFieldDescriptors>
{{endregion}}
The data binding validation can be enabled also by using the data annotation attributes.
{{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}}
{{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.
- [Getting Started]({%slug radcardview-getting-started%})
- [Events]({%slug radcardview-events%})
- [Visual Structure]({%slug radcardview-visual-structure%})