locked
Validate data with some behavior. RRS feed

  • Question

  • Greetings. Guys, can you tell me - what I do wrong?

    So, I have some Entity Framework model in my business application. From DataBase in DataGrid loading content, one of columns is "FullName" (Second name + First name + Middle name). Below datagrid I have a simplest form (NOT DataForm):

    <TextBlock Text="Full Name"/>
                            <TextBox Text="{Binding Path=FullName, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True,
                            ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
                            <TextBlock Text="Second Name"/>
                            <TextBox Text="{Binding Path=SecondName, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True,
                            ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
                            <TextBlock Text="First Name"/>
                            <TextBox Text="{Binding Path=FirstName, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True,
                            ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
                            <TextBlock Text="Middle Name"/>
                            <TextBox Text="{Binding Path=MiddleName, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True,
                            ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>

    There isn't Fields "First/Second/Middle name" in DataBase. I did implemet them into partial class at web-side of application and marked as:

    [DataMember]
    public string MiddleName {get; set;}


    Other fields like this. 

    Then, I made one more partial metadata class where I setted metadata and logic of validation:

    [MetadataType(typeof(CustomerMetadata))]
        public partial class Customer
        {
           internal sealed class CustomerMetadata
            {            private CustomerMetadata()
                {
                    
                }
     
                [StringLength(30, ErrorMessage="Full Name length error.", MinimumLength=3)]
                public string FullName { get; set; } // - This field exists in DataBase
     
                [RegularExpression(@"(^[A-Z][a-z]{2,19})",
                    ErrorMessage = "Second Name must start from capital letter and have length from 3 till 20 letters.")]
                public string SecondName { get; set; } // - This field is not exist in DataBase, I created it.
                //Other fields like this
            }
        }

    There all ok - validation works fine.

    BUT, if I want add some logic of filling data - namely, file Full Name won't be visible; user will be able to see and change 3 fields - Second/First/Middle name, and, it needs, that after successfully validation any of fields, the Full Name field must be changed automatically to (Second Name field)+" "+(First Name field)+" "+ (Middle Name field). The question is very trivial. 

    So I tried make this by next: I changed implementing of my fields in my first partial class (where I did write [DataMember] attribute):

    private string middleName;
            [DataMember]
            public string MiddleName
            {
                get { return middleName; }
                set
                {
                    middleName = value;
                    UpdateFullName("MiddleName", value);
                }
            }

    Other fields like this. And added one method below:

    private void UpdateFullName(string whatname, string name)
            {
                string[] localFullName = FullName.Split(' ');
                if (whatname == "SecondName") localFullName[0] = name;
                if (whatname == "FirstName") localFullName[1] = name;
                if (whatname == "MiddleName") localFullName[2] = name;
                FullName = localFullName[0] + " " + localFullName[1] + " " + localFullName[2];
            }

    But it didn't work... Also, I tried to fix it by:
    - Realize that method (with fields implementing) in metadata class - doesn't work too. Also, the validation too doesn't work with this changes.
    - I deleted first partial class and created same name shared.cs class, where I wrote fields without attribute [DataMember] (because Visual Studio throw exception with this) and wrote logic "UpdateFullName". The changing FullName fields and his validation work fine (when user change one of names), BUT, no any validation for Second/First/Middle name fields, wherever it has been described (into shared class or metadata class), application just ignore's them...

    So I need make validation + some_logic (behavior). But everytime I getting only one of them - or validation, or logic. Please, tell me, what I do wrong???

    Monday, November 5, 2012 7:26 PM

All replies

  • Getting confused on what you talking about, can you please just upload a simplified reproduce project to skydrive?
    Friday, November 9, 2012 8:14 AM
  • I'm a bit losts as to what you are trying to do. Could you break it down please:

    What would you like to do in terms of the name - one field in the database, and 3 fields for your end user? what rules for splitting the one record into 3 (I'm missing what you are trying to achieve)?

    Validation - what are you trying to validate and with what rule - does the full name have to have something? or all 3 names must have something?


    Phil Teare www.TheChaletExperience.com

    Thursday, November 15, 2012 10:59 AM