Hi
I am trying to perform custom validation on a User Control that I've created. I've tried following the examples and instructions to create a custom validation rule but am not having any success. You can see the full sample in the attached sample project.
Please could you point me in the right direction with respect to what is needed in the Form and/or the User Control to get the validation provider to work on this?
Thanks
Ian Edwards
Using the DX Validation Provider with a custom User Control
Answers
Hi Ian,
Thank you for the additional clarification.
DXValidationProvider cannot be used for non-BaseEdit descendants. If you wish to validate your UserControl, I suggest that you handle the Validating event. If you wish to show an error icon when a validation rule fails, you can simply place PictureEdit onto LayoutControl and set its EditValue property to the static BaseEdit.DefaultErrorIcon field value. You need to conditionally show/hide this icon as required by setting the BaseLayoutItem.Visibility property to Always/Never.
Please let me know if you need any further assistance.
Thanks,
Svetlana
Hi Svetlana
Thanks for your feedback. That's a helpful solution. I was hoping for something more automated, but understand that this is not viable with the control that we are using.
Appreciate the assistance.
Regards
Ian
An alternative is to create a custom validation rule.
Then create a dummy baseedit and set it's tag property to the control you want to validate.
For Example,
Validation Rule that inherits from ValidationRule:
Public Class ValidateTreeListMustContainASelectionRule
Inherits DevExpress.XtraEditors.DXErrorProvider.ValidationRule
Public Overrides Function Validate(control As Control, value As Object) As Boolean
Dim tl As DevExpress.XtraTreeList.TreeList = control.Tag
Me.ErrorText = "Must make at least one selection"
If tl.GetAllCheckedNodes().Count < 1 Then
Return False
Else
Return True
End If
End Function
End Class
Usage:
Dim valRule as new ValidateTreeListMustContainASelectionRule()
Dim baseEdit as new BaseEdit()
baseEdit.Tag = theTreeList 'Does not inherit from baseedit
dxErrorProvider.SetValidationRule(baseEdit, valRule)
Hi Ian,
Thank you for your message.
I do not quite understand your task. Please note that DXValidationProvider allows you to perform data validation against BaseEdit descendants. In your sample you are using UserControl with TreeView controls. DXValidationProvider cannot be used for data validation in TreeView. Could you please clarify these points?
Thanks to additional information we will be able to help you faster.
I am looking forward to your response.
Thanks,
Svetlana
Hi
I am creating a custom rule to validate the contents of my control using:
public class CostTypesValidationRule : ValidationRule
{
public override bool Validate(Control control, object value)
{
return (((SelectorControl)control).ItemCountRight != 0);
}
}
So this function will return false if the ItemCountRight property of the control = 0; or true if it is != 0. I was hoping that I could register this CustomValidationRule against the validation control. What I'm wanting to achieve is that the Icon shows if the ItemCount == 0.
Do I need my control to inherit from BaseEdit in order for it to work with the ValidationProvider? If so, can you provide some info as to how I do this.
Thanks
Ian
Hi Ian,
Please give us additional time. We will answer you ASAP.
Thanks,
Svetlana