FormatException thrown on RegularExpressionAttribute

3 posts, 0 answers
  1. Lukas
    Lukas avatar
    7 posts
    Member since:
    Sep 2012

    Posted 14 Apr 2014 Link to this post

    Hi

    I'm having an issue with the RadGridView in conjunction with the RegularExpressionAttribute. The grid has the following code:

    <telerik:RadGridView Grid.Row="3"
                         Grid.Column="0"
                         Grid.ColumnSpan="5"
                         AutoExpandGroups="True"
                         AutoGenerateColumns="False"
                         CanUserDeleteRows="False"
                         CanUserFreezeColumns="False"
                         CanUserInsertRows="False"
                         CanUserSelect="True"
                         CanUserSortColumns="False"
                         CanUserSortGroups="False"
                         IsFilteringAllowed="False"
                         ItemsSource="{Binding Templates}"
                         SelectedItem="{Binding SelectedTemplate, Mode=TwoWay}"
                         ShowGroupPanel="False">
        <telerik:RadGridView.GroupDescriptors>
            <telerik:GroupDescriptor Member="Category.Name" />
        </telerik:RadGridView.GroupDescriptors>
        <telerik:RadGridView.Columns>
            <telerik:GridViewColumn>
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="2" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="2" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
     
                            <Button Command="{Binding DataContext.EditTemplateCommand, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType=telerik:RadGridView}}" CommandParameter="{Binding}">
                                <Image Width="16"
                                       Height="16"
                                       Source="/Common;component/Images/Icons/edit.png"
                                       Stretch="Fill" />
                            </Button>
     
                            <Button Grid.Column="2"
                                    Command="{Binding DataContext.CopyTemplateCommand, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType=telerik:RadGridView}}"
                                    CommandParameter="{Binding}">
                                <Image Width="16"
                                       Height="16"
                                       Source="/Common;component/Images/Icons/copy.png"
                                       Stretch="Uniform" />
                            </Button>
     
                            <Button Grid.Column="4"
                                    Command="{Binding DataContext.RemoveTemplateCommand, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType=telerik:RadGridView}}"
                                    CommandParameter="{Binding}">
                                <Image Width="16"
                                       Height="16"
                                       Source="/Common;component/Images/Icons/trash.png"
                                       Stretch="Fill" />
                            </Button>
     
                        </Grid>
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>
            <telerik:GridViewDataColumn Width="*"
                                        DataMemberBinding="{Binding Title}"
                                        Header="{Binding Title, Source={StaticResource Strings}}" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>


    The entity looks like this:

    public class DocumentTemplate
    {
        [Key]
        public int Id { get; set; }
     
        [StringLength(50)]
        [RegularExpression(@"^[^\\~#%&*{}/:<>?|\" + "\"" + "-]+$", ErrorMessageResourceType = typeof(ValidationStrings), ErrorMessageResourceName = "TemplateTitleInvalid")]
        public string Title { get; set; }
     
        [StringLength(100)]
        public string FileName { get; set; }
     
        public string Xaml { get; set; }
     
        [Include, Association("DocumentTemplate_DataSources", "Id", "TemplateId")]
        public ICollection<DocumentDataSource> DataSources { get; set; }
     
        [Include, Association("DocumentTemplate_Category", "CategoryId", "Id")]
        public DocumentTemplateCategory Category { get; set; }
        public int CategoryId { get; set; }
     
        public DocumentTemplate()
        {
            DataSources = new HashSet<DocumentDataSource>();
        }
    }

    The entity is transmitted to the Silverlight client with RIA Services. I  tested the regular expression in a console application and it works there. When I replace the regex with ".*" it works. How can I fix this? I really need regex validation on that property.

    Lukas
  2. Lukas
    Lukas avatar
    7 posts
    Member since:
    Sep 2012

    Posted 14 Apr 2014 in reply to Lukas Link to this post

    I'm sorry, I just notice that I forgot to say what goes wrong: I'm getting an exception when loading the view:


    Type: FormatException
    Message: Input string was not in a correct format.

    Stacktrace:

       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
       at System.String.Format(IFormatProvider provider, String format, Object[] args)
       at System.ComponentModel.DataAnnotations.RegularExpressionAttribute.FormatErrorMessage(String name)
       at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
       at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
       at System.ComponentModel.DataAnnotations.Validator.TryValidate(Object value, ValidationContext validationContext, ValidationAttribute attribute, ValidationError& validationError)
       at System.ComponentModel.DataAnnotations.Validator.GetValidationErrors(Object value, ValidationContext validationContext, IEnumerable`1 attributes, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.GetObjectPropertyValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.GetObjectValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.TryValidateObject(Object instance, ValidationContext validationContext, ICollection`1 validationResults, Boolean validateAllProperties)
       at Telerik.Windows.Data.SafeValidator.TryValidateObject(Object instance, ValidationContext validationContext, ICollection`1 validationResults, Boolean validateAllProperties)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.<PerformDataAnnotationValidationForItem>d__71.MoveNext()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
       at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.GetAllDataErrorsForItem(Object dataItem)
       at Telerik.Windows.Controls.GridView.GridViewRow.GetErrors(Object item, GridViewDataControl owningDataGrid)
       at Telerik.Windows.Controls.GridView.GridViewRow.PrepareRow(Object item, GridViewDataControl owningDataGrid)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.PrepareContainerForItemOverride(DependencyObject element, Object item)
       at Telerik.Windows.Controls.GridView.BaseItemsControl.Telerik.Windows.Controls.GridView.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
       at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.NestedLayoutStrategy.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.NestedLayoutStrategy.InsertNewContainer(Int32 childIndex, UIElement container)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.NestedLayoutStrategy.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.NestedLayoutStrategy.GenerateNextChild(IItemContainerGenerator generator, Int32 childIndex)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.NestedLayoutStrategy.MeasureOverride(Size constraint)
       at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
  3. DevCraft banner
  4. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 16 Apr 2014 Link to this post

    Hello Lukas,

    I have already answered to your support ticket. In order to make our communication consistent, it would be better to continue our communication regarding this issue there.

    Regards,
    Yoan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top