Validation Problems

2 posts, 0 answers
  1. Armin
    Armin avatar
    11 posts
    Member since:
    Feb 2011

    Posted 15 Nov 2013 Link to this post

    Hello,

    i have any Problems with the MaskedInput Control.

    Scenario 1:

    Validation Error Messages not showed by Application Start.
    My Custom ControlTemplate for Validation.ErrorTemplate is showed,
    but the ErrorMessage is Empty. This is a little bit funny.

    You can see the Validation Methods in my TestModel Code.

    Scenario 2:
    My TestModel-Object has ProjectDescription Property as string with MaximumLength 80 and MinimumLength of 5.
    Enter 5 Characters in the MaskedInput Field, you can not deleting this Characters.
    Only overwrite from first Position is allowed.

    But... you can deleting the Characters, when the Count of Characters in the Field < Definition of MinimumLength is.
    All other Characters at Position > MinimumLength is deletable.

    MainWindow.xaml

    <Window
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="Telerik_Forum.MainWindow"
            xmlns:numericinput="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input"
            Title="MainWindow" Height="150" Width="525">
        <Window.Resources>
            <ControlTemplate x:Key="CustomErrorTemplate">
                <StackPanel Orientation="Horizontal">
                    <Border Margin="-1"
                        VerticalAlignment="Center">
     
                        <AdornedElementPlaceholder x:Name="Holder" />
                    </Border>
     
                    <Border Margin="3 1 0 1" HorizontalAlignment="Left" VerticalAlignment="Center" >
                        <StackPanel >
     
                            <TextBlock Text="!!!" ToolTip="{Binding ElementName=Holder,  Path=AdornedElement.DisplayErrorMessage}" />
                        </StackPanel>
                    </Border>
     
                </StackPanel>
     
            </ControlTemplate>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="Project Number:"/>
            <telerik:RadMaskedNumericInput Grid.Column="1" Grid.Row="0" Width="100" Validation.ErrorTemplate="{StaticResource CustomErrorTemplate}" HorizontalAlignment="Left" VerticalAlignment="Top" Value="{Binding ProjectNumber, ValidatesOnDataErrors=True, ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsClearButtonVisible="False" Mask="#4" AutoFillNumberGroupSeparators="False" Placeholder=" "/>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="Cost Summary:"/>
            <telerik:RadMaskedNumericInput Grid.Column="1" Grid.Row="1" Width="100" Validation.ErrorTemplate="{StaticResource CustomErrorTemplate}" HorizontalAlignment="Left" VerticalAlignment="Top" Value="{Binding CostSummary, ValidatesOnDataErrors=True, ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsClearButtonVisible="False" Mask="#9.2" AutoFillNumberGroupSeparators="False" Placeholder=" "/>
            <TextBlock Grid.Column="0" Grid.Row="2" Text="Project Description:"/>
            <telerik:RadMaskedTextInput Grid.Column="1" Grid.Row="2" Width="300" AllowInvalidValues="True" UpdateValueEvent="PropertyChanged" Validation.ErrorTemplate="{StaticResource CustomErrorTemplate}" HorizontalAlignment="Left" VerticalAlignment="Top" Value="{Binding ProjectDescription, ValidatesOnDataErrors=True, ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Mask="" IsClearButtonVisible="False" numericinput:MaskedInputExtensions.MinTextLength="5" numericinput:MaskedInputExtensions.MaxTextLength="80" Placeholder=" "/>
        </Grid>
    </Window>

    MainWindow.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
     
            TestModel m_Model = new TestModel(0, 0, "Test");
     
            this.DataContext = m_Model;
     
        }
    }

    TestModel.cs

    public class TestModel : IDataErrorInfo
    {
        public TestModel()
        { }
     
        public TestModel(int project_number, double cost_summary, string project_description)
        {
            this.ProjectNumber = project_number;
            this.CostSummary = cost_summary;
            this.ProjectDescription = project_description;
        }
     
        public int ProjectNumber { get; set; }
        public double CostSummary { get; set; }
     
        [StringLength(10,  MinimumLength = 5)]
        public string ProjectDescription { get; set; }
     
        public string Error
        {
            get { return this[string.Empty]; }
        }
     
        public string this[string propertyName]
        {
            get
            {
                string validationResult = null;
                switch (propertyName)
                {
                    case "ProjectNumber":
                        validationResult = Validate_ProjectNumber();
                        break;
     
                    case "CostSummary":
                        validationResult = Validate_CostSummary();
                        break;
     
                    case "ProjectDescription":
                        validationResult = Validate_ProjectDescription();
                        break;
     
                    default:
                        throw new ApplicationException("Unknown Property being validated on Product.");
                }
     
                return validationResult;
            }
        }
     
        private string Validate_ProjectNumber()
        {
            string Result = "";
     
            if (ProjectNumber == 0)
            {
                Result = "Please Enter a Project Number.";
            }
     
            if (ProjectNumber < 1 || ProjectNumber > 9999)
            {
                Result = "Only ProjectNumber Between 1 and 9999";
            }
     
            return Result;
        }
     
        private string Validate_CostSummary()
        {
            string Result = "";
     
            if (CostSummary == 0)
            {
                Result = "Please Enter Cost Summary Value.";
            }
            else if (CostSummary < 0.0)
            {
                Result = "Cost Summary allows only positive Values.";
            }
     
            return Result;
        }
     
        private string Validate_ProjectDescription()
        {
            string Result = "";
     
            if (ProjectDescription.Length == 0)
            {
                Result = "Please Enter a Project Description";
            }
            else if (ProjectDescription.Length < 5)
            {
                Result = "You must enter a Project Description with 5 Characters.";
            }
     
            return Result;
        }
    }
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 20 Nov 2013 Link to this post

    Hi Armin,

    We are aware of this behavior in the MaskedInput and we have previously logged a feature request for it.
    This includes also scenarios with IDataErrorInfo where the validation is triggered when the bindings are evaluated on load. We will try to include an improvement for this in the Service Pack coming in a several weeks. This will either work automatically or with some property/event / protected method.

    As for the MaskedInputExtensions.MinTextLength property , it is currently designed to support scenarios in which the Value initially is within the expected Range. Could you please elaborate more on what you expect to happen initially (probably autocomplete) and on deleting which may cause smaller lenght ?



    Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top