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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:46:05+00:00 2026-05-12T16:46:05+00:00

Apologies for cross posting (I asked this on the Silverlight Forum but got no

  • 0

Apologies for cross posting (I asked this on the Silverlight Forum but got no response)

I have an entity that I am trying to use validation on so I have decorated a property like so:

[Required]
[StringLength(10)]
public string Code
{
get
{
return this.code;
}

set
{
if (this.code != value)
{
this.code = value;
this.SendPropertyChanged("Code");
}
}
}

I have a list of these objects bound to a grid. If I put an empty entry in, it shows a validation error. If I put too long a code in, I get a validation error. Perfect! Except…

I want to be able to stop the user from saving the entity so I added the following to my entity:

public bool IsValid()
{
    try
    {
        this.Validate();
    }
    catch
    {
        return false;
    }
    return true;
}

public void Validate()
{
    var ctx = new ValidationContext(this, null, null);
    Validator.ValidateObject(this, ctx);
}

And when i go to save I call the IsValid method on each object and not save if it’s false.
This works fine for the required attribute (it wont save if Code is empty) but not for StringLength (I can save with any length code).

I have reproduced this in a simple project here:

http://walkersretreat.co.nz/files/Slvalidation.zip

Can anyone help?

Thanks!

Mark

  • 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-12T16:46:06+00:00Added an answer on May 12, 2026 at 4:46 pm

    you should write as:

    [CustomValidation( typeof( MyExtraClassValidation ), "Validate" )]
    public class MyExtraClass : Entity, IEditableObject, INotifyPropertyChanged
    {
       /****/
    }
    
    
    public class MyExtraClassValidation 
    {
        public MyExtraClassValidation ()
        {}
    
        public static ValidationResult Validate( MyExtraClass myExtraClass )
        {
            if ( /**class is not valid*/)
                return new ValidationResult( "Oops" );
    
            return ValidationResult.Success;
        }
    
    }
    

    Of course, your interfaces may be ablolutely anorther, but I reccomend you use it.

    Also, you can call validateHandler from your control and check, for example, after every user key pressing.

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

Sidebar

Related Questions

Apologies if this question is a bit daft, but are there any negative repercussions
Apologies if this ends up being a stupid question, but I was just wondering
Apologies for using someone else's brain for this, but I'm sure this is a
Apologies for the appalling title. I have mocked up this code to mimic an
apologies if this is a dupe; i couldn't find it. i've read and understood
Apologies, I'm new to ASP.NET MVC. :) I'm unsure if this is simple a
I've got an Enum called DoYouKnow, containing Yes , No and Unknown . I
Apologies for the title, I found it hard to define my question succintly and
Apologies for the long post and I hope it makes some sense to someone
Apologies for the code dump: gameObject.cpp: #include gameObject.h class gameObject { private: int x;

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.