Validation with EF4.1 and DataAnnotations

3 posts, 1 answers
  1. Mike
    Mike avatar
    39 posts
    Member since:
    May 2011

    Posted 29 Jun 2011 Link to this post

    i have spent the last two days two find out how the validation works in the dataform. My DAL uses Entity Framework 4.1 with auto generated POCO Entities (in a seperate Assembly).
    I'm working with MVVM and have UserControls for my views. One UserControl has a RadDataFrom with customized fields. The currentItem is bound to a an instance of my POCO Entities. Now i'd like to use DataAnnotations in the entities for my validatioin. But the error are not displayed in the dataform. I don't know what i'm doing wrong, can you please help me?
    Here is the UserControl containing the dataform and the corresponding POCO:

    <UserControl xmlns:telerik=""  x:Class="MiexProto.Views.EmployeeFormView"
                 d:DesignHeight="300" d:DesignWidth="300">
            <DataTemplate x:Key="EmployeeTemplate">
                    <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
                        <Label Content="Anrede"/>
                        <telerik:DataFormComboBoxField Height="auto"  DataMemberBinding="{Binding AnredeId, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" SelectedValuePath="AnredeId" DisplayMemberPath="Bezeichnung" ItemsSource="{Binding ElementName=me, Path=DataContext.ListAnrede}" />
                    <StackPanel Orientation="Horizontal"  Grid.Row="0" Grid.Column="1">
                        <Label Content="Titel" />
                        <telerik:DataFormComboBoxField Height="Auto" DataMemberBinding="{Binding Titel.Bezeichnung, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" DisplayMemberPath="Bezeichnung" ItemsSource="{Binding ElementName=me, Path=DataContext.ListTitel}" />
                    <telerik:DataFormCheckBoxField Grid.Row="0" Grid.Column="2" Label="Deaktiviert" DataMemberBinding="{Binding Deaktiviert, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
                    <telerik:DataFormDataField Grid.Row="1" Grid.Column="0" Label="Nachname" DataMemberBinding="{Binding Nachname, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
                    <telerik:DataFormDataField Grid.Row="1" Grid.Column="1" Label="Vorname" DataMemberBinding="{Binding Vorname, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
                    <telerik:DataFormDataField Grid.Row="2" Grid.Column="0" Label="Durchwahl" DataMemberBinding="{Binding Durchwahl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
                    <telerik:DataFormDataField Grid.Row="2" Grid.Column="1" Label="Mobilnummer" DataMemberBinding="{Binding Mobilnummer, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" />
            <telerik:RadDataForm CurrentItem="{Binding Employee}" Header="{Binding Employee.Nachname}" AutoEdit="True" CommandButtonsVisibility="None" EditTemplate="{StaticResource EmployeeTemplate}" AutoGenerateFields="False" />

    namespace Entities
        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        public partial class Mitarbeiter
            public Mitarbeiter()
                this.Veranstaltungsanmeldung = new HashSet<Veranstaltungsanmeldung>();
                this.Zustaendigkeit = new HashSet<Zustaendigkeit>();
            public int MitarbeiterId { get; set; }
            public int KundeId { get; set; }
            public int AnredeId { get; set; }
            public int TitelId { get; set; }
            public string Vorname { get; set; }
            public string Nachname { get; set; }
            public string Durchwahl { get; set; }
            public string Email { get; set; }
            [Required(AllowEmptyStrings = false, ErrorMessage = "Es muss eine Handynummer angegeben werden!")]
            [Display(Name = "Kunde")]
            [RegularExpression("^\\+(\\d+)\\s\\((\\d+)\\)\\s(\\d+)(\\s-\\s(\\d+))?", ErrorMessage = "Die Telefonnummer muss im Format +43 (XXX) XXXXX - XX sein")]
            public string Mobilnummer { get; set; }
            public bool Deaktiviert { get; set; }
            public string Bild { get; set; }
            public System.DateTime UpdateDatum { get; set; }
            public string UpdateBenutzer { get; set; }
            public virtual Anrede Anrede { get; set; }
            public virtual Kunde Kunde { get; set; }
            public virtual Titel Titel { get; set; }
            public virtual ICollection<Veranstaltungsanmeldung> Veranstaltungsanmeldung { get; set; }
            public virtual ICollection<Zustaendigkeit> Zustaendigkeit { get; set; }

  2. Answer
    Pavel Pavlov
    Pavel Pavlov avatar
    2039 posts

    Posted 05 Jul 2011 Link to this post

    Hi Michael,

    I have prepared a small sample illustrating how this can be achieved. Please find it attached. To give it a try - just run the project and try entering e.g. a number in the FirstName field.

    Pavel Pavlov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. Mike
    Mike avatar
    39 posts
    Member since:
    May 2011

    Posted 05 Jul 2011 Link to this post

    Thank's a lot, that did the job!
Back to Top