Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3483380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:40:04+00:00 2026-05-18T10:40:04+00:00

Please ask if you can’t understand what I’m asking. I have created a custom

  • 0

Please ask if you can’t understand what I’m asking.

I have created a custom ValidateAttribute for my ViewModel

i created it for validate properties which depend from another property of ViewModel

if (user checked “01” or “09” from QrupList) Then

  1. Company name is needed
  2. Name,surname and LastName are not needed

else

  1. Company name is not needed
  2. Name,surname and LastName are needed

I have ViewModel as below

[ValidateForGroupAttribute("Group", "CompanyName")]
public partial class AbonentViewModel
{      
    [DisplayName("Şirkət")]
    public string CompanyName { get; set; }

    [DisplayName("Soyadı")]
    [Required(ErrorMessage = "Soyadı vacibdir")]
    public string Surname { get; set; }      

    [DisplayName("Qrup")]
    [Required(ErrorMessage = "Qrup vacibdir")]
    public string Group{ get; set; }

    public SelectList GroupList { get; set; }
}

My custom ValidationAttribute classes:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class ValidateForGroupAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' a müvafiq '{1}' daxil din";

    public ValidateForGroupAttribute(string originalProperty, string confirmPropertyCompany)
        : base(_defaultErrorMessage)
    {
        OriginalProperty = originalProperty;
        ConfirmPropertyCompany = confirmPropertyCompany;
    }

    public string OriginalProperty { get; private set; }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            OriginalProperty, ConfirmPropertyCompany);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true).GetValue(value);
        object confirmValueCompany = properties.Find(ConfirmPropertyCompany, true).GetValue(value);

        if ((string)originalValue == "01" || (string)originalValue == "09")
            return false;
        else
            return true;
    }
}

How do I do it? What is wrong in my ValidationAttributes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:40:05+00:00Added an answer on May 18, 2026 at 10:40 am

    We looked at validation using data annotations a few months back, and decided it was better to use something like fluent validation, as we had complex business rules and logic that would have taken too much effort to realise with data annotations. Have a look at the documentation, and you will see fluent validation makes things like this easy.

    Sorry, I did not get back sooner: Check fluent validation here

    Your rule could look something like. Syntax not tested, but I am sure you will be able to figure it out.

    public class AbonentViewModelValidator : AbstractValidator<AbonentViewModel> {
        public AbonentViewModelValidator() {
            RuleFor(model => model.CompanyName).NotEmpty().When(model => (model.GroupList.Id == 1 || model.GroupList.Id == 9 ));
            RuleFor(model => model.Surname).NotEmpty().When(model => (model.GroupList.Id != 1 || model.GroupList.Id != 9 ));
            RuleFor(model => model.Name).NotEmpty().When(model => (model.GroupList.Id != 1 || model.GroupList.Id != 9 ));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mutipule web projects (please don't ask me why with multipule namespaces within
Before I ask my question can I please ask not to get a lecture
sorry for my bad english, if you don't understand me, please ask! :) I
This question is complex, so please ask questions for explaining more detail on this
Please note that I did ask this on Super User and received absolutely no
I am a newbie in cmd, so please allow me to ask a stupid
I'm from SQL world, so please, don't ask me that there are no tables,
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have an application where different users can log in via a single portal
I have an extremely odd problem in my JUnit tests that I just can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.