Validation

34 posts, 0 answers
  1. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 02 Feb 2012 Link to this post

    1) I am interested how to use validation based on RadPropertyGrid integrated Attributes ?
    2) How to mix telerik Attributes with Microsoft Attributes with Property Grid?

    I would like to be MVVM conform !

    Example of Validation with Property Grid would be nice.

    Thank you,
    Branko
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Feb 2012 Link to this post

    Hello Branko,

    We have introduced additional DataAnnotations support for RadPropertyGrid that will be included in our next official release Q1 2012. It will work as expected - for example defining a property with Browsable false will make the property not be generated in the grid:

    private string name;
     
            [Browsable(false)]
            public string Name
            {
                get { return this.name; }
                set
                {
                    if (value != this.name)
                    {
                        this.name = value;
                        this.OnPropertyChanged("Name");
                    }
                }
            }



    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 03 Feb 2012 Link to this post

    Hello Maya,

    [Browsable] is one of the Attribute, what are the other attributes that we can work with ?

    I am validating RadPropertyGrid control and I am trying to set it up in a proper way. I need to work with attributes. Example, is when AutoGeneratePropertyDefinitions="True", Auto Generated Properties in the Property Grid are generated based on, input type of variables.

    How to handle error state and validation using attributes, if the input property is not correct. Is not in the range, or has expression which is not correct, etc ?

    How to write Description for the property, but not from xaml !?

    <telerik:RadPropertyGrid AutoGeneratePropertyDefinitions="True" Item="{Binding Path=DataElement}">
    </telerik:RadPropertyGrid>
     
    // I would like to define range for the type of DateTime as Attribute . If the date is not in
    // the proper range, show predefined or customized ErrorMessage,
    // as a tooltip, marking the field in red. Similar to RadMaskedDateTimeInput control.
               
                    private DateTime dateTimeValue;
         

                    [Range(typeof(DateTime), "01.01.1900", "01.01.2099")]

               
    public DateTime DateTimeValue
                {
                    get { return dateTimeValue; }
                    set
                    {
                        dateTimeValue = value;
                        OnPropertyChanged("DateTimeValue");
                    }
                }


    Thank you,
    Kind Regards,
    Branko
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 08 Feb 2012 Link to this post

    Hi Branko,

    We have added support for IsReadOnly, Browsable, Description, ValidatesOnException, NotifyOnValidationErrors, Display attributes.
    You can define the description like follows:

    [Browsable(true)]
            [Description("The department the employee is associated with.")]
            [Display(Name = "Department", Order = 2, GroupName = "Job Description")]
            [ReadOnly(true)]
            public int DepartmentId
            {
                get
                {
                    return departmentId;
                }
                set
                {
                    if (this.departmentId != value)
                    {
                        this.departmentId = value;
                    }
                }
            }
     

    All the best,
    Maya
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 15 Feb 2012 Link to this post

    Hallo Maya,

    Thank you for response. I have one more question. In new release, will we be able to write our own attributes ?

    Thank you.
    Best regards,
    Branko
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 16 Feb 2012 Link to this post

    Hello Branko,

    Could you provide a bit more details about the exact scenario that you want to accomplish ?  

    Greetings,
    Maya
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 17 Feb 2012 Link to this post

    Hello Maya,
    We would like to accomplish something like this.

    public bool IsLastNameRequired { ... }
      
    [RequiredIf("IsLastNameRequired", true, ErrorMessage = "Last name is required.")]
    public string LastName
    {
        get { return this.lastName; }
        set
        {
            this.lastName = value;
            this.OnPropertyChanged("LastName");
        }
    }

    [RequiredIf (..)] is custom attribute. And we can use it for validating.

    Do you support IDataErrorInfo inteface in RadPropertyGrid ?

    Thank you.
    Best regards,
    Branko



  8. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 17 Feb 2012 Link to this post

    Hello Branko,

    If you target implementing custom validation based on attributes, our recommended approach is to use the CustomValidationAttribute. I have prepared an example project for you that demonstrates how to achieve this. However, you should use 2012.1.215, or a newer version, in order to benefit from this feature. As a side note, please let me bring to your attention that we have planned to introduce a few validation improvements, including a validation summary for RadPropertyGrid.

    All the best,
    Ivan Ivanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  9. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 21 Feb 2012 Link to this post

    Hello Ivan,

    Do you support IDataErrorInfo interface in Property Grid? This interface is very appropriate to use for validation in MVVM design.

    Please take a look at this example, similar solution we want to achieve implemented in Property Grid. This is nice example of self written attribute ([RequiredIf("IsLastNameRequired", true, ErrorMessage = "Last name is required.")]), and validation of that attribute.

    http://blog.cylewitruk.com/2010/10/cross-field-attribute-validation-in-wpf-using-mvvm-and-idataerrorinfo/

    Looking forward your kind answer. Thank you,
    Best regards
    Branko

  10. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 21 Feb 2012 Link to this post

    Hi Branko,

    Currently RadPropertyGrid does not support DataErrors validation (It supports exception validation only). However, as it is not a major feature we can implement it for you and include it in an internal build. I believe that we will manage to introduce this change in two weeks. I will keep in touch with you, in order to notify you as soon as the mentioned IB is available for download.

    Regards,
    Ivan Ivanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  11. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 23 Feb 2012 Link to this post

    Hello Ivan,

    Thank you for the answer. I am waiting your internal build. In the meanwhile I would like to try Attributes that you have integrated in this last release. Where I can find more about attributes, type of attributes that you are currently supporting in Property Grid?

    P.S. The goal of my project is, by selecting the object to present properties of selected object: example ObjA (5 properties), ObjB (10 properties), ObjC(7 properties). Type of properties can be, bool, decimal, float, string etc. And then apply validation on those properties from selected object. As I have mention we are MVVM oriented, and validating those properties would be nice with IDataErrorInfo interface.

    If you have any other suggestion on this scenario please inform me.
    Thank you,

    Best regards,
    Branko
  12. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 23 Feb 2012 Link to this post

    Hello Branko,

    The following demo: demos > RadPropertyGrid > "DataAnnotations support" illustrates RadPropertyGrid's native compatibility with several attributes: ReadOnly, Display, Description, Category, Browsable. The control also supports exception based property validation, so that you can implement validaition logic using the DataAnnotatins validation attributes (Required, Range, Regex, CustomValidation etc.). You can refer to the attached project from the previous post that illustrates how to implement validation based on the CustomValidation attribute. Basicly attribute-based validation should be fully MVVM comliant, so that I believe that it might be an appropriate approach in your scenario.

    Kind regards,
    Ivan Ivanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  13. Branko
    Branko avatar
    18 posts
    Member since:
    Feb 2012

    Posted 29 Feb 2012 Link to this post

    Hello Ivan,

    We are waiting your Internal Build for DataErrors validation. Just to clarify one more time, we would like if you could implement PropertyGrid interaction with IDataErrorInfo interface, and we will then make our error handling using that interface and Property Grid.

    Regards,
    Branko
  14. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 29 Feb 2012 Link to this post

    Hi Branko,

    I will notify you as soon as the IB is released (it should be the next Monday/Tuesday). I will prepare a simple test project for your referense, as well. Please, do not hesitate to contact us if you have any further inquiries. 

    Kind regards,
    Ivan Ivanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  15. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 07 Mar 2012 Link to this post

    Hello Branko,

    I have prepared an example project for you with a type that implements IDataErrorInfo, using the latest IB binaries. As I have mentioned in the other support thread of yours, we are up to add a validation summary to RadPropertyGrid, so that users could have a summarized view of errors' messages.

    Kind regards,
    Ivan Ivanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  16. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 25 Apr 2013 Link to this post

    How is it going with support for IDataErrorInfo? I have have tried the WpfApplication135 with Q1 sp1 2013 and the age rule is not working.
  17. Yoan
    Admin
    Yoan avatar
    1211 posts

    Posted 25 Apr 2013 Link to this post

    Hi Martin,

    You can handle RadPropertyGrid's AutoGeneratingPropertyDefinition event and execute the following code:

    void radPropertyGrid_AutoGeneratingPropertyDefinition(object sender, Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e)
           {
               (e.PropertyDefinition.Binding as Binding).ValidatesOnDataErrors = true;
               (e.PropertyDefinition.Binding as Binding).NotifyOnValidationError = true;
           }

    I have modified the sample project with our latest official binaries and now it is working as expected. You can find it attached.


    Kind regards,
    Yoan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  18. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 25 Apr 2013 Link to this post

    (-: I have already done that, inspired by another post in this forum.

    But I can not get it to work with "normal" validation attributes.

    [Required(ErrorMessage = "Name is required")]
    [RegularExpression(@"^(?:\w*\d*[_]*)$", ErrorMessage = "Input format is: a-z, A-Z, 0-9, _")]
    public string Name { get; set; }
  19. Yoan
    Admin
    Yoan avatar
    1211 posts

    Posted 25 Apr 2013 Link to this post

    Hello Martin,


    You can manually apply the validation by using the Validator class. You can call the ValidateProperty method on the set accessor of a property to check the value against the validation attributes for the property. Please note that you must also set both ValidatesOnExceptions and NotifyOnValidationError properties to true when data binding to receive validation exceptions from validation attributes. The following code snippet demonstrates how to use data annotation attributes to validate a property value:
    [Range(6,26)] 
    public int Age        
           {
               get { return this.age; }
               set
               {
                   Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age"});              
                   age = value;
               }
           }


    Regards,
    Yoan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  20. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 25 Apr 2013 Link to this post

    All our business objects that needs validation are already implementing IDataErrorInfo and we are using FluentValidation for all the rules.
    We can then use the same object in MVC4 and WPF without writing ugly code in the setters
    For propertychanged we are using Fody. so every property is just plain get;set;

    Please implement IDataErrorInfo as you promised a year ago, you can read it her

    Happy coding


     
  21. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 01 May 2013 Link to this post

    Hello,

    Could you please be more specific? You are mentioning both IDataErrorInfo and the DataAnnotations validation attributes? Are using the latter in order to validate and retrieve the errors in the IDataErrorInfo error indexer? As far as I can see a colleague of mine has sent you a sample project with IDataErrorInfo and it is working fine. As for this FluentValidation framework, we cannot claimsuppor for it.

    Kind regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  22. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 01 May 2013 Link to this post

    Yes of course, But it's all boils down to the conversation you had with Branko one year ago where you promised to implement IDataErrorInfo

    Hi Branko,

    I will notify you as soon as the IB is released (it should be the next Monday/Tuesday). I will prepare a simple test project for your referense, as well. Please, do not hesitate to contact us if you have any further inquiries. 

    Kind regards,
    Ivan Ivanov 
    the Telerik team

    If you implement the interface IDataErrorInfo you can sit back and let the developer implement the error handling any way he choose.

    I don't think that I should be forced to write code like this:
    [Range(6,26)]
    public int Age       
    {
         get { return this.age; }
          set
           {
                   Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age"});             
                   age = value;
             }
     }

    I want this
     public Int Age {get; set;} 

     

      
  23. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 01 May 2013 Link to this post

    Fell free to call me on +0045 5383 5556
  24. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 07 May 2013 Link to this post

    Hi,

     I am afraid that for the time being we cannot suggest you a phone support. RadPropertyGrid supports IDataErrorInfo as the entire control's functionality relies on data binding and the framework Binding natively supports DataError notifications. In case you want to use both DataAnnotation attributes and IDataErrorInfo and you want to use DataError validation rather than exception-based validation, you can use the Validator.TryValidateObject method in order to retrieve the errors and pass them through IDataErrorInfo errors indexer. It should be something like this:

    public string this[string columnName]
    {
        get
        {
            List<ValidationResult> results = new List<ValidationResult>();
            Validator.TryValidateObject(this, new ValidationContext(this, null, null), results);
            if (results.FirstOrDefault(r => r.MemberNames.Contains("Name")) != null)
            {
                return ". . .";
            }                                              
            else
            {
                return string.Empty;
            }
        }
    }

    Regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  25. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 07 May 2013 Link to this post

    Thanks for the support so far, it's has been a long journey, but I think we are getting to the end of the road.

    From your last post I could read that the propertyGrid support the IDataErrorInfo interface so I created a small test project to test it out.

    This is what I found:
    • If I bind to a single object then validation is working, in PropertySetMode="Intersection" and PropertySetMode="None"  mode.
    • If I bind to an ObservableCollection with two object, then validation is not working any more.

    Whit two or more object I know that you are creating a dynamic object and I think that this dynamic object is not implementing IDataErrorInfo.

    Another problem with the dynamic object are that it's propagate changes out but it's not listen to changes made to the object.

    Are we getting closer to a real problem definition?
    Just for fun I have attached a picture of the form designer I am building. I am close to be finish but I am missing validation and true 2-way binding in the PG.
  26. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 10 May 2013 Link to this post

    Hi Martin,

    Well, it makes sense. I have not figured that you are using the PropertySets feature. The current implementation of this feature utilizes an internal implementation of DynamicObject to represent the union/intersection set of dynamic properties. Another weakness of this approach is the lack of attribute support. Unfortunately, introducing a general internal implementation of IDataErrorInfo, will be quite a difficult task as validation relies on rules that are scenario-specific. We had some ideas about reworking this feature by using ICustomTypeProvider and adding more customization options to the public API. I will be able to tell you more on this matter as soon as we are with the Q2 development. We are going to do some research on this feature and will contact you back until the end of May.

    Regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  27. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 16 May 2013 Link to this post

    I have been thinking about how to implement IDataErrorInfo. Here are some ideas 

    First we need a setting on the PG: ValidateOnAllPropertySetModes = "True"

    Then maybe a call-back before a value is set: string (object valueToSet, IList<object> listOfSelectedObjects) that returns string.empty if there is no errors, and a string with the error description if there is an error.

    If the call-back is not set then resolve the errors like this.

    We have selected two object that both has public int Height {set;get;}  property.
    Object one has this rules
    • Required == true
    • Interval between 10 and 200

    Object two has this rule
    • maxheight = 200

    Test's
    Set Height = 5
    obj2 is happy, obj1 is not, so the input value 5 is rejected and the error message for obj2 is displayed.

    set Height = 10
    Everybody is happy, set the value.

    I would like to help you out with coding or as a tester if it can speed up the process.






  28. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 16 May 2013 Link to this post

    Just to let you know, Mindscape has a wpf propertygrid that can do all of this.

    • Validate more than one object at a time. Using IDataErrorInfo
    • easy way to disable a property editor based on a value in another property editor
    • You don't have to leave the property editor before the value is set on the underlying object.
    • If the underlying object is manipulated the changes are reflected in the PropertyGrid.

    Are U up for the challenge? 
  29. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 16 May 2013 Link to this post

    Hi,

     Thank you for expressing your requirements and ideas, Martin. We are almost done with the development of Q2, so we will be able to research this feature in the close future. We have come with an idea to expose an API that would import of a custom validation ruleset, which would be used by our DynamicObject's IDataErrorInfo implementation. I will send you a demo as soon as we have made any significant progress.

    Kind regards,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  30. Martin
    Martin avatar
    39 posts
    Member since:
    Sep 2008

    Posted 17 May 2013 Link to this post

    Just don't forget to make it work with objects that's already implements IDateErrorInfo, don't make us write extra code.
Back to Top