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 7505301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:51:49+00:00 2026-05-29T21:51:49+00:00

I’m just getting to grips with custom validation attributes, and I’m trying to write

  • 0

I’m just getting to grips with custom validation attributes, and I’m trying to write a custom validation attirbute which will be placed at class level to validate against multiple properties of my model.

I can access all properties on my model, and I want to be able to check for multiple conditions in my IsValid overload, and report on them, having different error messages as follows (simplistic example).

public override bool IsValid(object value)
    {
        var model = (MyObject) value;

        //if this value is set, I don't want to do anything other checks
        if (model.Prop3)
        {
            return true;
        }

        if (model.Prop1 == "blah" && model.Prop2 == 1)
        {
            ErrorMessage = "you can't enter blah if prop 2 equals 1";
            return false;
        }

        if(model.Prop1 == "blah blah" && model.Prop2 == 2)
        {
            ErrorMessage = "you can't enter blah blah if prop 2 equals 2";
            return false;
        }


        return true;
    }

But when I do this I get an exception on the first time ErrorMessage is referenced “Cannot set property more than once.

Now I could split up my custom attribute into multiple custom attributes, but hoped there would be a way to do it in one, otherwise, I’ll be repeating my “catch all” in each

//if this value is set, I don't want to do anything other checks
        if (model.Prop3)
        {
            return true;
        }

I’ve had a search already, but couldn’t find anything, so apologies if I am missing anything obvious.

thanks in advance!

  • 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-29T21:51:52+00:00Added an answer on May 29, 2026 at 9:51 pm

    Interesting question! I can think of two work-arounds to this. So not proper solutions based on what you want but they might help to re-use your code. Cant you create a CustomAttribute abstract class called MyCustomAttribute (or something) that overrides IsValid in the following way:

    public override bool IsValid(object value)
    {
        var model = (MyObject) value;
    
        //if this value is set, I don't want to do anything other checks
        if (model.Prop3)
        {
            return true;
        }
    
        CustomValidate(model);
    }
    

    CustomValidate(MyObject model) is your abstract method then, you can write multiple custom attribute classes that extend MyCustomAttribute and purely need to implement the validation logic for A particular scenario.

    So you can have two classes:

    public class BlahCustomAttribute : MyCustomAttribute
    {
        public override Boolean CustomValidate(MyObject obj)
        {
            if (model.Prop1 == "blah" && model.Prop2 == 1)
            {
                ErrorMessage = "you can't enter blah if prop 2 equals 1";
                return false;
            }
        }
    }
    
    public class BlahBlahCustomAttribute : MyCustomAttribute
    {
        public override Boolean CustomValidate(MyObject obj)
        {
            if (model.Prop1 == "blah" && model.Prop2 == 1)
            {
                ErrorMessage = "you can't enter blah blah if prop 2 equals 1";
                return false;
            }
        }
    }
    

    Hope this helps – not exactly what you wanted but will do the job and its clean as well.

    The other solution is to comma-separate the error messages in the ErrorMessage property and handle it in the front-end (but I would go with the first approach).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.