Ticket Q328714
Visible to All Users

Using the DX Validation Provider with a custom User Control

created 14 years ago

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

Comments (3)
DevExpress Support Team 14 years ago

    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

      DevExpress Support Team 14 years ago

        Hi Ian,
        Please give us additional time. We will answer you ASAP.
        Thanks,
        Svetlana

        Answers

        created 14 years ago

        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

          Comments (2)

            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

            ML ML
            Michael Lindsey 6 years ago

              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)

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.