This is a migrated thread and some comments may be shown as answers.

Validation with EF4.1 and DataAnnotations

2 Answers 156 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 2
Mike asked on 29 Jun 2011, 03:45 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Pavel Pavlov
Telerik team
answered on 05 Jul 2011, 01:41 PM
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!

0
Mike
Top achievements
Rank 2
answered on 05 Jul 2011, 08:16 PM
Thank's a lot, that did the job!
Tags
DataForm
Asked by
Mike
Top achievements
Rank 2
Answers by
Pavel Pavlov
Telerik team
Mike
Top achievements
Rank 2
Share this question
or