Ticket T512034
Visible to All Users

Data Annotation conditional required attribute

created 8 years ago

I'm wondering if the DataLayoutControl supports or has a way to toggle required fields.

Say I have a data model as such

C#
public enum CustomerType { User, Reseller } public class DataModel { public string FirstName { get; set; } public string LastName { get; set; } public CustomerType CustomerType { get; set; } = CustomerType.User; [Required(ErrorMessage = "Reseller name can not be null or empty.")] // need this to be conditional based on CustomerType. public string ResellerName { get; set; } }

If i can set this required field manually that will work as well.

Please let me know.

Answers approved by DevExpress Support

created 8 years ago (modified 8 years ago)

Hi,

It is possible to accomplish this task by creating a custom RequiredAttribute. Override the RequiresValidationContext property to return true. In addition, override the IsValid method to return ValidationResult.Success if the CustomerType property value is User.

C#
[CustomRequiredAttribute(ErrorMessage = "Reseller name can not be null or empty.")] public string ResellerName { get; set; }
C#
public class CustomRequiredAttribute : RequiredAttribute { public CustomRequiredAttribute() { } public CustomerType ConditionParameter { get; set; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { DataModel model = validationContext.ObjectInstance as DataModel; if (model != null && model.CustomerType == CustomerType.User) return ValidationResult.Success; return base.IsValid(value, validationContext); } public override bool RequiresValidationContext { get { return true; } } }

I should note that this approach will operate properly in .Net Framework 4.5 and higher since in the earlier .Net Framework versions, the RequiredAttribute class does not have the public virtual RequiresValidationContext property.

Please try the approach and let me know your results.

    Comments (2)

      I had originally tried this with out
      RequiresValidationContext = trueAfter setting this and refreshing the databindings this works as expected. Thank you. I have another question i'll go ahead and make a new topic for sense I can't seem to find it.

      Thank you.

      DevExpress Support Team 8 years ago

        Thank you for informing us that the issue has been resolved. Please do not hesitate to contact us in case of any difficulty. We will be happy to help you!

        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.