Validation with DataTemplates

7 posts, 0 answers
  1. Jeff Buechler
    Jeff Buechler avatar
    6 posts
    Member since:
    Feb 2010

    Posted 08 Sep 2011 Link to this post

    It seems that validation only works when using AutoGenerateFields. When using DataTemplates the field with an error is flagged but the form fires the EditEnded event and returns to the read only state in spite of the error. Is there any way around this? Will it be fixed in a coming release?
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 09 Sep 2011 Link to this post

    Hello Jeff Buechler,

    Would you please shed some more light on the validation logic you have applied? Do you use Data Annotations attributes, or some of the validation interfaces (i.e. IDataErrorInfo).

    Kind regards,
    Ivan Ivanov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
  4. vk
    vk avatar
    46 posts
    Member since:
    Jul 2012

    Posted 09 Sep 2011 Link to this post

    Try setting ValidatesOnExceptions and NotifyOnValidationError to true for each binding.
    <telerik:DataFormDateField Label="Load date"
    DataMemberBinding="{Binding LoadDate, Mode=TwoWay,
    ValidatesOnExceptions=True, NotifyOnValidationError=True}" />

  5. Jeff Buechler
    Jeff Buechler avatar
    6 posts
    Member since:
    Feb 2010

    Posted 09 Sep 2011 Link to this post

    In my actual project I'm using Data Annotations. I've also tried using IDataErrorInfo by modifying a sample provided in response to another post. The only difference was that with Data Annotations I did get the error message to appear. With IDataErrorInfo, there was no indication at all of validation. Below is the code from the modified sample. Am I missing something?

    <UserControl.Resources>
            <DataTemplate x:Key="editTemplate">
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Prop1" />
                        <TextBox Text="{Binding Prop1, Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Prop2" />
                        <TextBox Text="{Binding Prop2, Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="readOnlyTemplate">
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Prop1" />
                        <TextBox Text="{Binding Prop1, Mode=TwoWay}" IsReadOnly="True" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Prop2" />
                        <TextBox Text="{Binding Prop2, Mode=TwoWay}" IsReadOnly="True" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
     
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadDataForm x:Name="radDataForm"
                                 AutoGenerateFields="False"
                                 EditTemplate="{StaticResource editTemplate}"
                                 ReadOnlyTemplate="{StaticResource readOnlyTemplate}"
                                 NewItemTemplate="{StaticResource editTemplate}"
                                 >
                 
            </telerik:RadDataForm>
        </Grid>
    </UserControl>
     
     
    namespace SilverlightApplication202
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                radDataForm.ItemsSource = new ObservableCollection<TestItem>() { new TestItem() { Prop1 = "77", Prop2 = "43" } };
                radDataForm.AutoGeneratingField += new EventHandler<Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs>(radDataForm_AutoGeneratingField);
            }
     
            void radDataForm_AutoGeneratingField(object sender, Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs e)
            {
                if (e.PropertyName == "Error")
                {
                    e.Cancel = true;
                }
                e.DataField.DataMemberBinding.ValidatesOnDataErrors = true;
                e.DataField.DataMemberBinding.NotifyOnValidationError = true;
            }
        }
     
        public class TestItem : IDataErrorInfo
        {
            public string Prop1 { get; set; }
            public string Prop2 { get; set; }
     
            public string Error
            {
                get { return null; }
            }
     
            public string this[string columnName]
            {
                get
                {
                    string error = "";
                    if (this.Prop1 == "" || this.Prop1 == null )
                    {
                        error = "Prop1 is required";
                    }
                    if (this.Prop2 == "" || this.Prop2 == null)
                    {
                        error = "Prop1 is required";
                    }
                    return error;
                }
            }
        }
     
    }

  6. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 14 Sep 2011 Link to this post

    Hello Jeff Buechler,

    Please, excuse us for the inconvenience. This is a known issue with RadDataForm. We will provide a solution for it in one of our internal builds in the close future.

    Best wishes,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  7. Nick Wood
    Nick Wood avatar
    126 posts
    Member since:
    Feb 2010

    Posted 02 Aug 2012 Link to this post

    Has this error been fixed becuase I am playing around with the CRM demo source and cannot get the DateTimePicker to raise any DataValidations, but the same time picker does raise data validations notifications outside of the DataForm.

    Nick
  8. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 03 Aug 2012 Link to this post

    Hi Nick,

    Would you please, confirm what type of Validation you are using? The previous posts in this thread refer to the IDataErrorInfo-based validation, but such is not implemented on our CRM demo. Moreover, you should set the Binding's Validation properties (ValidatesOnDataErrors/Exceptions) by yourself, when controls different from the DataField controls are used in some of RadDataForm's templates. Would it be possible for you to isolate the issue in a sample project and send it to us, so that we can debug it on our side?

    All the best,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner