raddataform, validation and Required annotation

9 posts, 1 answers
  1. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 02 Aug 2011 Link to this post

    Re code below.
    If I enter a character in the age field I get an error flagged ... OK
    If I enter a name starting with B I get an error flagged ... OK
    It also picks up the annotation "Display" and sets title to "User Name" ... OK
    But I can't get the "Required" annotation to cause an error when name is blank ?
    Note: I tried the standard RadForm and that also does not pick up on "Required"
    Should it work, am I missing something ?
    Thanks
    Jim


    <Grid x:Name="LayoutRoot">
        <telerik:RadDataForm HorizontalAlignment="Left"  Name="DataForm1" Height="408" Width="449" />
    </Grid>



    Public Class Person
      
        Private m_name As String
        <Display(name:="User Name")>
        <Required(ErrorMessage:="Name is required.")>
        Public Property name As String
            Set(n As String)
                If n.StartsWith("B") Then
                    Throw New ValidationException("Can't start with B")
                Else
                    m_name = n
                End If
      
            End Set
            Get
                Return (m_name)
            End Get
        End Property
      
        Public Property age As Integer
        Public Sub New(n As String, a As Integer)
            name = n
            age = a
        End Sub
    End Class
  2. Answer
    Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 02 Aug 2011 Link to this post

    Hi Jim,

    RadDataForm has no native support of the "Required" data annotation attribute yet. We plan this feature to be added in some of our future releases. For now, I would advise you to implement IDataErrorInfo on your business object class, in order to achieve your task. I have prepared an example project that illustrates this approach. An alternative solution is to explicitly validate your property regarding the attributes set like this:
    [Required(ErrorMessage="User Name is Required")]
     public string UserName
            {
                get { return userName; }
                set 
                {
                    Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName="UserName"});
                    userName = value; 
                }
            }

    Regards,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  3. DevCraft banner
  4. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 02 Aug 2011 Link to this post

    Thanks for the info
    Will have a look tomorrow.
    Regards
    Jim
  5. Brian
    Brian avatar
    58 posts
    Member since:
    Jul 2011

    Posted 27 Jan 2012 Link to this post

    Ivan,
        Thanks for the sample.  I would like one of my fields to be required, and I would like the form to indicate that it is required right away when a new object is created.  I can force the required message to appear on the RadDataForm by validating my object when it is created, but the OK button on the RadDataForm is still enabled in this case.  However, if I enter data in the field, tab away, then go back and clear the field and tab away, I not only get the required message, the OK button is disabled as well.  Is there a way to get the OK button to be disabled either by default, or in response to my validation when the object is created?

    Thanks,
    Brian
  6. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 31 Jan 2012 Link to this post

    Hi Brian,

    We have introduced some improvements in RadDataForm validation with Q3 2011. Now there is item level validation that is carried out on commit, navigate etc. However, this validation errors do not disable the commit button - only property level validation errors do it. This behavior is similar to toolkit DataForm's validation behavior. I presume that you invoke Validator.ValidateProperty in your property setters, so the only way to cause a property level error is to access the respective setter.

    All the best,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  7. Brian
    Brian avatar
    58 posts
    Member since:
    Jul 2011

    Posted 31 Jan 2012 Link to this post

    Ivan,
    Actually, my class is a RIA services Entity, so the code-generated properties call Entity.ValidateProperty in the setters.  When I set the value of my property to a non-valid value, the red outline and validation message appear on the field, but the OK button is not disabled.  According to the documentation (http://msdn.microsoft.com/en-us/library/ff422361(v=vs.91).aspx), this method should throw ValidationException just like Validator.ValidateProperty, and the fields in the data form appear to recognize that the property value did not pass validation, but the OK button only seems to respond when the value I have set is actually entered into the field on the data form.  This behavior occurs in both the Q2 2011 and Q3 2011 releases.

    I have a working example of this if you would like to see it.

    Thanks,
    Brian
  8. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 03 Feb 2012 Link to this post

    Hello,

     Please send us the project via support ticket.

    All the best,
    Vlad
    the Telerik team

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

  9. Kirk
    Kirk avatar
    1 posts
    Member since:
    Feb 2012

    Posted 23 Feb 2012 Link to this post

    I'm seeing a very similar problem to what Brian had.  Was there a resolution for that?
  10. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 24 Feb 2012 Link to this post

    Hello Kirk,

    Would you please confirm which version of RadControls you are curently using? We have introduced some validation improvements for RIA Entities with Q1 2012 and I believe that this problem should be fixed with the mentioned version.

    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 >>
Back to Top
DevCraft banner