Validation with EF4.1 and DataAnnotations

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

    Posted 29 Jun 2011 Link to this post

    Hi,
    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="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="MiexProto.Views.EmployeeFormView"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d"
                 x:Name="me"
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <DataTemplate x:Key="EmployeeTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <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>
                    <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}" />
                    </StackPanel>
                    <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}" />
                </Grid>
            </DataTemplate>
     
        </UserControl.Resources>
        <Grid>
            <telerik:RadDataForm CurrentItem="{Binding Employee}" Header="{Binding Employee.Nachname}" AutoEdit="True" CommandButtonsVisibility="None" EditTemplate="{StaticResource EmployeeTemplate}" AutoGenerateFields="False" />
        </Grid>
    </UserControl>

    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
    Admin
    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.

    Regards,
    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. UI for WPF is Visual Studio 2017 Ready
  4. Mike
    Mike avatar
    38 posts
    Member since:
    May 2011

    Posted 05 Jul 2011 Link to this post

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