Issue with Validation in WPF - wpf

I have applied the validations of required field validation on the wpf textbox, using the ValidationRule class. My code is :
public class RequiredField : ValidationRule
{
private String _errorMessage = String.Empty;
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var str = value as string;
if (String.IsNullOrEmpty(str))
{
return new ValidationResult(false, this.ErrorMessage);
}
return new ValidationResult(true, null);
}
}
And the XAML code is below :
<TextBox Name="txtName" MaxLength="50">
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<myValidtaion:RequiredField ErrorMessage="Please enter Name." />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
This seems to work fine. But the issue is that after i save the valid data in database and clear the database, this validation is fired again and validation message appears again.
How can I avoid this situation ?

This seems to be a common validation 'problem', but it is in fact the expected behviour of WPF validation... this also occurs when using the IDataErrorInfo interface.
Both of these validation methods will validate their specified properties whenever their values change, which is exactly what we want (most of the time).
I believe that the system is called predictive validation and it enables us to show the user what they have to fill in before they try to save. I personally believe that this is a better system than the old system of letting the user try to save fields with errors and then telling them afterwards that they have made an error.
Perhaps you can delay clearing the fields until the user clicks a New button, in which case the errors will only show up then?
UPDATE >>>
If you were to use the IDataErrorInfo interface, then you could add a bool IsValidating property to your data type classes that you could use to switch validation on and off. I don't have much time, so I found a post that implements this interface that I have adapted to demonstrate my point.
public class Person : IDataErrorInfo
{
private int age;
private bool isValidating = true;
public int Age
{
get { return age; }
set { age = value; }
}
public bool IsValidating
{
get { return isValidating; }
set { isValidating = value; }
}
public string Error
{
get
{
return this["Age"];
}
}
public string this[string name]
{
get
{
string result = null;
if (IsValidating)
{
if (name == "Age")
{
if (this.age < 0 || this.age > 150)
{
result = "Age must not be less than 0 or greater than 150.";
}
}
}
return result;
}
}
}
Adapted from the code in the accepted answer to Creating WPF Validation error manually post
When you switch this IsValidating property to false, the property changes won't be validated:
person.IsValidating = false;

Related

Enable Disable save button during Validation using IDataErrorInfo & Update Button State

I am new to the WPF MVVM and wanted to ask a follow up question to this article:
Enable Disable save button during Validation using IDataErrorInfo
I am trying to enable/disable the button save/update if any of the many controls on the form validation failed/passed.
I have the IsValid method, that checks the validation logic on the Model and returns True/False, that will be passed on to the DelegateCommand as a predicate.
The question is: my button has the following property IsEnabled{binding IsValid}, this should check all fields to make sure that it matches the criteria in the model, returns true/false to the view model and then enables the button if all true. The problem is: Once the view model is instantiated, the DelegateCommand object is created with the validation (IsValid) at at a false state and it stays that way throughout the life of the object even though the user is filling data in the textboxes. How do I turn on the button once all the conditions are met? in other words, how to constantly keep validating and updating the IsValid in order to switch the button on once every textbox validate to true??
Thanks,
I have the following code:
The Model
public class UserModel : ObservePropertyChanged,IDataErrorInfo
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
// if there is an error throw an exception
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
result = "Please enter a Name";
}
return result;
}
}
// the Josh Way
static readonly string[] ValidatedProperties =
{
"Name"
};
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null) // there is an error
return false;
}
return true;
}
}
// a method that checks validation error
private string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "Name":
error = this.ValidateName();
break;
default:
error = null;
throw new Exception("Unexpected property being validated on Service");
}
return error;
}
private string ValidateName()
{
string ErrorMsg = null;
if (string.IsNullOrWhiteSpace(Name))
{
ErrorMsg = "Name can't be empty!";
};
return ErrorMsg;
}
}
** the View Model **
public class UserViewModel:ObservePropertyChanged
{
UserModel model;
public UserViewModel()
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
model = new UserModel();
}
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
private string info;
public string Info
{
get { return info; }
set { info = value; OnPropertyChanged("Info"); }
}
private DelegateCommand presentCommand;
public DelegateCommand PresentCommand
{
get
{
if (presentCommand==null)
{
presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
}
return presentCommand;
}
}
private void PresentDataMethod()
{
Info = $"Your Name is: {Name}.";
}
// The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:
protected bool CanSave
{
get
{
return model.IsValid;
}
}
}
** The View**
<TextBox x:Name="Name" HorizontalAlignment="Left" Height="34" Margin="285,145,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="248">
<Binding Path="Name"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
Mode="TwoWay">
</Binding>
</TextBox>
<Button Content="Present" FontSize="20" HorizontalAlignment="Left"
Margin="285,184,0,0" VerticalAlignment="Top" Width="248" Height="35"
Command="{Binding Path=PresentCommand}"
IsEnabled="{Binding IsValid}"
>
</Button>
If all you want to do is refresh the button via the IsValid value, all you have to do is listen for any of the OTHER property changes in your ViewModel, and when that happens, tell it to refresh the IsValid binding as well (which is actually your CanSave property). Here is one way to do that:
** The View Model **
// ...
public UserViewModel()
{
// ...
this.PropertyChanged += OnViewModelPropertyChanged;
}
public void OnViewModelPropertyChanged(object sender, PropertyEventArgs e)
{
// On any property that implements "OnPropertyChanged(propname)", refresh the CanSave binding too!
OnPropertyChanged(nameof(this.CanSave));
}
// ...
BTW, it is generally good coding practice to avoid magic words like OnPropertyChanged("Name") or OnPropertyChanged("Info") because you never know when a developer will have to rename their properties, and if that happens, you won't get a compile error here and it might be hard to debug. It is best practice to use the nameof, such as OnPropertyChanged(nameof(Name)) so that you'll know you'll get a compilation error if you ever decide to change the property to, say, FirstName instead of Name.

Binding to a ValidationRule in WPF

I am new to WPF. The form's data context includes a StartTime and an EndTime field (using MVVM), which I have successfully bound to their own text boxes. I am trying to create a validation to check that a new user-entered StartTime is before the EndTime value. The following code does not seem to bind the EndTime field to the validation parameter Maximum.
XAML:
<TextBox>
<TextBox.Text>
<Binding Path="StartTime" UpdateSourceTrigger="LostFocus" StringFormat="{}{0:hh}:{0:mm}">
<Binding.ValidationRules>
<local:ValidateTime>
<local:ValidateTime.Maximum>
<local:ValidationParameter Parameter="{Binding EndTime, StringFormat=hh\\:mm}" />
</local:ValidateTime.Maximum>
</local:ValidateTime>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
View model:
public class ValidationParameter : DependencyObject
{
public static readonly DependencyProperty ParameterProperty = DependencyProperty.Register(
"Parameter",
typeof(string),
typeof(ValidationParameter),
new FrameworkPropertyMetadata(null));
public string Parameter
{
get { return (string)GetValue(ParameterProperty); }
set { SetValue(ParameterProperty, value); }
}
}
public class ValidateTime : ValidationRule
{
private TimeSpan _Minimum = new TimeSpan(0, 0, 0);
private TimeSpan _Maximum = new TimeSpan(23, 59, 9);
private ValidationParameter _MinimumProperty;
private ValidationParameter _MaximumProperty;
public ValidationParameter Minimum
{
get
{
return _MinimumProperty;
}
set
{
TimeSpan ts;
if (TimeSpan.TryParse(value.Parameter, out ts))
{
_Minimum = ts;
_MinimumProperty = value;
}
}
}
public ValidationParameter Maximum
{
get
{
return _MaximumProperty;
}
set
{
TimeSpan ts;
if (TimeSpan.TryParse(value.Parameter, out ts))
{
_Maximum = ts;
_MaximumProperty = value;
}
}
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string formattedValue = value.ToString();
if (Regex.IsMatch(formattedValue, #"^\d{4}$"))
{
formattedValue = string.Format("{0}:{1}", formattedValue.Substring(0, 2), formattedValue.Substring(2, 2));
}
TimeSpan convertedValue;
if (TimeSpan.TryParseExact(formattedValue, "g", System.Globalization.CultureInfo.CurrentCulture, out convertedValue))
{
if (convertedValue > _Maximum)
{
return new ValidationResult(false, string.Format("Time must be before {0}.", _Maximum.ToString("g")));
}
else if (convertedValue < _Minimum)
{
return new ValidationResult(false, string.Format("Time must be after {0}.", _Minimum.ToString("g")));
}
return ValidationResult.ValidResult;
}
else
{
return new ValidationResult(false, string.Format("'{0}' is not a valid time entry.", value.ToString()));
}
}
}
The code works if I set the parameter to a static value like the following, but I need this validation to be dynamic:
<local:ValidateTime.Maximum>
<local:ValidationParameter Parameter="12:00" />
</local:ValidateTime.Maximum>
First you want to give your TextBox a name like so:
<TextBox Name="StartTime"/>
Then you set the parameter for validation like so:
<local:ValidateTime.Maximum>
<local:ValidationParameter Parameter="{Binding ElementName=StartTime, Path=Text" />
</local:ValidateTime.Maximum>
If the validation works like you said you should be all set.
You can do the same thing with your EndTime TextBox if you want as well assuming you write a working rule for it
You probably need to work around the fact that ValidationRules are not in the visual or logical trees, which causes bindings to DataContext, ElementName and RelativeSource to fail.
In my case
<local:RangeValidation ValidationStep="UpdatedValue"/>
helped. So you pass the DependencyObject as value(.dataitem) including your Datacontext and have no need to pass a parameter to the Validation.
BR,
Daniel
BTW credits not on mee, just found it but can't remember who gave the hint :)

Validation by comparing the properties of one entity against another

Here is what I have:
In the view there is a tab control with two tabs (sys1 and sys2) each with the same textboxes that are bound to the properties of their respective entities:
sys1:
<TextBox Text="{Binding sys1.Serial, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
sys2:
<TextBox Text="{Binding sys2.Serial, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
Using some form of validation I would like to compare the two values and display an error (red border is fine) if the values don't match.
I've used IDataErrorInfo before, but I'm not sure if this type of validation is possible.
Note: whether or not binding directly to the entity is "correct" is a discussion for another place and time. Just know that this is a team project and our teams standards are to bind to the entity so I can't change that unless I have a good reason. Perhaps if it's not possible validating when bound directly to the entity then I may have a good enough reason to change this.
Thanks
I usually expose a Validation Delegate from my Model that my ViewModel can use to attach business-rule validation to the Models.
For example, the ViewModel containing your objects might look like this:
public ParentViewModel()
{
sys1.AddValidationErrorDelegate(ValidateSerial);
sys2.AddValidationErrorDelegate(ValidateSerial);
}
private string ValidateSerial(object sender, string propertyName)
{
if (propertyName == "Serial")
{
if (sys1.Serial == sys2.Serial)
return "Serial already assigned";
}
return null;
}
The idea is that your Model should only contain raw data, therefore it should only validate raw data. This can include validating things like maximum lengths, required fields, and allowed characters. Business Logic, which includes business rules, should be validated in the ViewModel and this allows that to happen.
The actual implementation of my IDataErrorInfo on the Model class would look like this:
#region IDataErrorInfo & Validation Members
/// <summary>
/// List of Property Names that should be validated
/// </summary>
protected List<string> ValidatedProperties = new List<string>();
#region Validation Delegate
public delegate string ValidationErrorDelegate(object sender, string propertyName);
private List<ValidationErrorDelegate> _validationDelegates = new List<ValidationErrorDelegate>();
public void AddValidationErrorDelegate(ValidationErrorDelegate func)
{
_validationDelegates.Add(func);
}
#endregion // Validation Delegate
#region IDataErrorInfo for binding errors
string IDataErrorInfo.Error { get { return null; } }
string IDataErrorInfo.this[string propertyName]
{
get { return this.GetValidationError(propertyName); }
}
public string GetValidationError(string propertyName)
{
// If user specified properties to validate, check to see if this one exists in the list
if (ValidatedProperties.IndexOf(propertyName) < 0)
{
//Debug.Fail("Unexpected property being validated on " + this.GetType().ToString() + ": " + propertyName);
return null;
}
string s = null;
// If user specified a Validation method to use, Validate property
if (_validationDelegates.Count > 0)
{
foreach (ValidationErrorDelegate func in _validationDelegates)
{
s = func(this, propertyName);
if (s != null)
{
return s;
}
}
}
return s;
}
#endregion // IDataErrorInfo for binding errors
#region IsValid Property
public bool IsValid
{
get
{
return (GetValidationError() == null);
}
}
public string GetValidationError()
{
string error = null;
if (ValidatedProperties != null)
{
foreach (string s in ValidatedProperties)
{
error = GetValidationError(s);
if (error != null)
{
return error;
}
}
}
return error;
}
#endregion // IsValid Property
#endregion // IDataErrorInfo & Validation Members
P.S. I see nothing wrong with binding directly to the Model, especially in smaller applications. It may not be the "MVVM-purist" approach, however it is efficient and a lot less work, so I find it a perfectly valid option.
In the set (mutator) for sys1.Serial1 and sys2.Serial you should be able to get the other's value for a comparison.

WPF Binding : Use DataAnnotations for ValidationRules

I have read a lot of Blog post on WPF Validation and on DataAnnotations. I was wondering if there is a clean way to use DataAnnotations as ValidationRules for my entity.
So instead of having this (Source) :
<Binding Path="Age" Source="{StaticResource ods}" ... >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
Where you must have your
public class AgeRangeRule : ValidationRule
{...}
I want the WPF Binding to go see the Age property and look for DataAnnotation a bit like this:
[Range(1, 120)]
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged<...>(x => x.Age);
}
}
Any ideas if this is possible ?
The closest approach I found is :
// This loop into all DataAnnotations and return all errors strings
protected string ValidateProperty(object value, string propertyName)
{
var info = this.GetType().GetProperty(propertyName);
IEnumerable<string> errorInfos =
(from va in info.GetCustomAttributes(true).OfType<ValidationAttribute>()
where !va.IsValid(value)
select va.FormatErrorMessage(string.Empty)).ToList();
if (errorInfos.Count() > 0)
{
return errorInfos.FirstOrDefault<string>();
}
return null;
Source
public class PersonEntity : IDataErrorInfo
{
[StringLength(50, MinimumLength = 1, ErrorMessage = "Error Msg.")]
public string Name
{
get { return _name; }
set
{
_name = value;
PropertyChanged("Name");
}
}
public string this[string propertyName]
{
get
{
if (porpertyName == "Name")
return ValidateProperty(this.Name, propertyName);
}
}
}
Source and Source
That way, the DataAnnotation works fine, I got a minimum to do on the XAML ValidatesOnDataErrors="True" and it's a fine workaround of Aaron post with the DataAnnotation.
In your model you could implement IDataErrorInfo and do something like this...
string IDataErrorInfo.this[string columnName]
{
get
{
if (columnName == "Age")
{
if (Age < 0 ||
Age > 120)
{
return "You must be between 1 - 120";
}
}
return null;
}
}
You will also need to notify the binding target of the newly defined behavior.
<TextBox Text="{Binding Age, ValidatesOnDataErrors=True}" />
EDIT:
If you only want to use Data Annotations you can follow this blog post which outlines how to accomplish the task.
UPDATE:
Historical representation of the aforementioned link.
Sounds good Aaron. I'm just into WPF and will study databindings next week at work ;) So cannot completely judge your answer...
But with winforms I have used Validation Application Block from the Entlib and implemented IDataErrorInfo (actually IDXDataErrorInfo because we work with DevExpress controls) on a base entity (business object) and that works pretty fine!
It's a bit more sophisticated than the solution you sketched in this way that you place your validation logic on the object and not in the interface implementation. Making it more OOP and maintainable. At the ID(XD)ataErrorInfo I just call Validation.Validate(this), or even better get the validator for the property that the interface is called for and validate the specific validator. Don't forget to call the [SelfValidation] as well because of validation for combinations of properties ;)
You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It uses the DataAnnotations Validation attributes together with WPF Binding.
Recently I've had the same idea using the Data Annotation API to validate EF Code First POCO classes in WPF. Like Philippe's post my solution uses reflection, but all necessary code is included in a generic validator.
internal class ClientValidationRule : GenericValidationRule<Client> { }
internal class GenericValidationRule<T> : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string result = "";
BindingGroup bindingGroup = (BindingGroup)value;
foreach (var item in bindingGroup.Items.OfType<T>()) {
Type type = typeof(T);
foreach (var pi in type.GetProperties()) {
foreach (var attrib in pi.GetCustomAttributes(false)) {
if (attrib is System.ComponentModel.DataAnnotations.ValidationAttribute) {
var validationAttribute = attrib as System.ComponentModel.DataAnnotations.ValidationAttribute;
var val = bindingGroup.GetValue(item, pi.Name);
if (!validationAttribute.IsValid(val)) {
if (result != "")
result += Environment.NewLine;
if (string.IsNullOrEmpty(validationAttribute.ErrorMessage))
result += string.Format("Validation on {0} failed!", pi.Name);
else
result += validationAttribute.ErrorMessage;
}
}
}
}
}
if (result != "")
return new ValidationResult(false, result);
else
return ValidationResult.ValidResult;
}
}
The code above shows a ClientValidatorRule which is derived from the generic GenericValidationRule class. The Client class is my POCO class which will be validated.
public class Client {
public Client() {
this.ID = Guid.NewGuid();
}
[Key, ScaffoldColumn(false)]
public Guid ID { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "You have to provide a name.")]
public string Name { get; set; }
}

How to validate inputs when click button in silverlight?

Im using DataAnnotation to validate input controls. But ValidatesOnExceptions only works when user type something in textbox and press Tab. (Basically on Lostfocus event).
but if user never enters anything in the textbox and click submit. It does not work. Like ASP.NET Page.IsValid property is there any property or method in Silverlight that i can use, that will validates all the controls on UI?
Taking help from the URL provided by Terence, i have prepared the solution below for you.
This you can use to make sure that all the properties are set before service call.
public class PersonViewModel : EntityBase
{
private readonly RelayCommand saveCommand;
public PersonViewModel(IServiceAgent serviceAgent)
{
saveCommand = new RelayCommand(Save) { IsEnabled = true };
}
public RelayCommand SaveCommand // Binded with SaveButton
{
get { return saveCommand; }
}
public String Name // Binded with NameTextBox
{
get
{
return name;
}
set
{
name = value;
PropertyChangedHandler("Name");
ValidateName("Name", value);
}
}
public Int32 Age // Binded with AgeTextBox
{
get
{
return age;
}
set
{
age = value;
PropertyChangedHandler("Age");
ValidateAge("Age", value);
}
}
private void ValidateName(string propertyName, String value)
{
ClearErrorFromProperty(propertyName);
if (/*SOME CONDITION*/)
AddErrorForProperty(propertyName, "/*NAME ERROR MESSAGE*/");
}
private void ValidateAge(string propertyName, Int32 value)
{
ClearErrorFromProperty(propertyName);
if (/*SOME CONDITION*/)
AddErrorForProperty(propertyName, "/*AGE ERROR MESSAGE*/");
}
public void Save()
{
ValidateName("Name", name);
ValidateAge("Age", age);
if (!HasErrors)
{
//SAVE CALL TO SERVICE
}
}
}
I don't think, that there is a way to validate ALL UserControls which are visible on the page. But I would recommend you to have a look at the INotifyDataErrorInfo. This is, in my opinion, the best way to validate data in silverlight. With the INotifyDataErrorInfo approach you don't have to make changes in the view (like ValidatesOnException, ...) and you are able to validate against a WebService in an easy way (This is not possible with data annotations).
Have a look here: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-binding-with-inotifydataerrorinfo.aspx
Hope this helps you.

Resources