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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:42:22+00:00 2026-06-13T16:42:22+00:00

How do i catch the validation from the DataAnnotations ? i research here but

  • 0

How do i catch the validation from the DataAnnotations ?
i research here but i didn’t understand how it works

so i hoppe some of you can enlighten my

here my current test code:

Model

public class Person // Represents person data.
{
    /// <summary>
    /// Gets or sets the person's first name.
    /// </summary>
    /// <remarks>
    /// Empty string or null are not allowed.
    /// Allow minimum of 2 and up to 40 uppercase and lowercase.
    /// </remarks>
    [Required]
    [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]        
    public string FirstName{ get; set;}

    /// <summary>
    /// Gets or sets the person's last name.
    /// </summary>
    /// <remarks>
    /// Empty string or null are not allowed.
    /// </remarks>
    [Required]
    public string LastName { get; set;}

    public int Age{ get; set;}
}

View

<Window x:Class="DataAnnotationstest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataAnnotationstest"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:Person FirstName="Tomer" LastName="Shamam" />
    </Window.DataContext>
    <Grid>
        <StackPanel Margin="4,4,51,4">
            <TextBox Text="{Binding FirstName, ValidatesOnDataErrors=True}" />
            <TextBox Text="{Binding LastName, ValidatesOnDataErrors=True}" />
            <TextBox Text="{Binding Age, ValidatesOnDataErrors=True}" />
        </StackPanel>
    </Grid>
</Window>

do i need to implement something else to Person?
i found here the following code but like i said before i didn’t understand how it’s worke -.-

public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute
{
    var attrType = typeof(T);
    var property = instance.GetType().GetProperty(propertyName);
    return (T)property .GetCustomAttributes(attrType, false).First();
}
  • 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-06-13T16:42:23+00:00Added an answer on June 13, 2026 at 4:42 pm

    the solution

    public class Person : IDataErrorInfo // Represents person data.
    {
        /// <summary>
        /// Gets or sets the person's first name.
        /// </summary>
        /// <remarks>
        /// Empty string or null are not allowed.
        /// Allow minimum of 2 and up to 40 uppercase and lowercase.
        /// </remarks>
        [Required]
        [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]        
        public string FirstName{ get; set;}
    
        /// <summary>
        /// Gets or sets the person's last name.
        /// </summary>
        /// <remarks>
        /// Empty string or null are not allowed.
        /// </remarks>
        [Required]
        public string LastName { get; set;}
    
        public int Age{ get; set;}
    
        public string Error // Part of the IDataErrorInfo Interface
        {
            get { throw new NotImplementedException(); }
        }
    
     string IDataErrorInfo.this[string propertyName] // Part of the IDataErrorInfo Interface
        {
            get { return OnValidate(propertyName); }
        }
    
        /// <summary>
        /// Validates current instance properties using Data Annotations.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected virtual string OnValidate(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentException("Invalid property name", propertyName);
    
            string error = string.Empty;
            var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
            var results = new List<ValidationResult>(1);
    
            var context = new ValidationContext(this, null, null) { MemberName = propertyName };
    
            var result = Validator.TryValidateProperty(value, context, results);
    
            if (!result)
            {
                var validationResult = results.First();
                error = validationResult.ErrorMessage;
            }
    
            return error;
        }
    }
    

    thanks to Rachel for here hint
    and to this link which was very enlightened

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

Sidebar

Related Questions

i would like to get all validation Errors from the SAX-Parser, but with my
What have people used to catch, log, and report multiple data validation errors at
I can't get dataform and it's controls to catch exceptions when validating. Fir instance,
I need to catch or filter a number from string example: var Mystring =
i want to catch the data from server as an answer and alert it.
I am trying to convert some code from using DOM (via jDOM) to use
I'm having a validation issue that I can't figure out. I know I have
I am trying to write some validation code in my class for my GUI.
I have developed one login form in Android. I have used validation here. I
I'm trying to implement binding validation using Silverlight on Windows Phone, but all I

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.