Search code examples
c#wpfvalidationtelerikradwindow

Run validation when a user input something in a text field


What do I need to do, if I want the validation run when a user input something in a text field, not when the window pop up. Here is the example image: Product Window Textboxes

In the example image when I executed this window, it validates automatically. I want it to check the validation when I input something.

Here is my code snippet in validation:

public string Error
    {
        get { return null; }
    }

    public string this[string propertyName]
    {
        get
        {
            string error = string.Empty;
                switch (propertyName)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(Name))
                            error = "Name is required!";
                        break;
                    case "Url":
                        if (string.IsNullOrEmpty(Url))
                            error = "Url is required!";
                        else if (!Regex.IsMatch(Url, @"(?:https?:\/\/)?(?:[\w]+\.)([a-zA-Z\.]{2,6})([\/\w\.-]*)*\/?"))
                            error = "Url is invalid";
                        break;
                    case "Price":
                        if (Price < 0)
                            error = "Price cannot be negative!";
                        break;
                    default:
                        break;
                }

            return error;
        }
    }

Here is the code snippet in my UI:

 <!--Product Name-->
    <Label Content="Name:" />
    <TextBox x:Name="txtName" 
             Grid.Column="2"
             Validation.Error="ValidationError"
             Text="{Binding Name, 
                    Mode=TwoWay, 
                    ValidatesOnDataErrors=True,
                    NotifyOnValidationError=True,
                    UpdateSourceTrigger=PropertyChanged}" />
    <!--Product Url-->
    <Label Grid.Row="2" Content="Url:" />
    <TextBox x:Name="txtUrl" 
             Grid.Row="2" 
             Grid.Column="2"
             Validation.Error="ValidationError"
             Text="{Binding Url, 
                    Mode=TwoWay, 
                    ValidatesOnDataErrors=True,
                    NotifyOnValidationError=True,
                    UpdateSourceTrigger=PropertyChanged}"/>

Solution

  • If by "when I input something" you mean "everytime I press a key", then change your Binding UpdateSourceTrigger value to PropertyChanged on the TextBox. This will mean that whenever the TextBox's value is changed, it will automatically update the bound object and trigger the IDataErrorInfo interface calls to update the status of the TextBox.

    Update:

    You need to implement INotifyPropertyChanged interface to allow IDataErrorInfo to function correctly.

    public class MyObject : INotifyPropertyChanged, IDataErrorInfo
    {
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     private string _myValue;
     public string MyValue
     {
       get { return _myValue; }
       set 
       {
         _myValue = value;
         OnPropertyChanged("MyValue");
       }
     }
    
     private void OnPropertyChanged(string propertyName)
     {
         var handler = PropertyChanged;
         if (handler != null)
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
    
     public string Error
        {
            get { return null; }
        }
    
        public string this[string columnName]
        {
            get
            {
                string returnValue = null;
                switch (columnName)
                {
                    case "MyValue":
                       if MyValue != "expected"
                          returnValue = "MyValue is not expected";
                }
                return returnValue;
             }
        }
    
    }
    

    Note: IDataErrorInfo is only called when MyValue is retrieved after update notification to the UI (thus the get implementation instead of set in the indexer)