I am currently using data binding in my Windows Forms application.
I am taking the View Model approach which wraps all of my business functionality together. My data objects are Entity Framework POCOs which have all of the database metadata that includes maximum length, null etc. Each object implements the IDataErrorInfo interface. Entity Framework takes care of the field validation.
Now given that, I want to get the error messages into an UltraValidator. The only info I have is from 2009 on these forums which indicated that it did not support the interface. It would be great if it did although the IDataErrorInfo interface is not that great IMHO.
So my question is, I have lots of validation on different fields, what is the best way to do this.
So suppose I have a business object (without IDataErrorInfo and it implements INotifyPropertyChanged which is omitted):
public class MyPOCO
{
private string _name;
public string Name
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
}
private int _age;
public int Age
get { return _age; }
set { _age = value; OnPropertyChanged("Age"); }
This is bound to a TextBox and a Numeric UpDown:
this.NameTextbox.DataBindings.Add("Text", this.ViewModel.MyPOCO, "Name", true, DataSourceUpdateMode.OnPropertyChanged ); this.Age.DataBindings.Add("Text", this.ViewModel.MyPOCO, "Age", true, DataSourceUpdateMode.OnPropertyChanged );
I want to add all kinds of validation I guess. This will be:
Do I need a single UltraValidator instance or do I need to add many instances? I am currently using:
this.EntityValidator.GetValidationSettings(this.NameTextbox).EmptyValueCriteria = Infragistics.Win.Misc.EmptyValueCriteria.NullOrEmptyString; this.EntityValidator.GetValidationSettings(this.NameTextbox).IsRequired = true; this.EntityValidator.GetValidationSettings(this.NameTextbox).DataType = typeof(string); this.EntityValidator.GetValidationSettings(this.NameTextbox).ValidationTrigger = Infragistics.Win.Misc.ValidationTrigger.OnValidating; this.EntityValidator.GetValidationSettings(this.NameTextbox).NotificationSettings.Caption = "My Validation"; this.EntityValidator.GetValidationSettings(this.NameTextbox).NotificationSettings.Action = Infragistics.Win.Misc.NotificationAction.Image; this.EntityValidator.GetValidationSettings(this.NameTextbox).NotificationSettings.Text = "The Name is required.";
Can you give me some examples here on how to achieve this please. I've looked at the legacy examples which are pretty basic. What condition classes are available?
The example uses a RangeCondition which derives from an Object.
Thanks,
Andez
Hi Dimitar,
The supplied solution is what I was after. It works a treat. I thought I did respond to your solution but I guess not :/
Hi Andez,
I am just checking about the progress of this issue. Let me know if you need my further assistance on this issue.
Thank you for using Infragistics Components.
Thank you for contacting Infragistics Developer Support.
You need only one instance of the UltraValidator even if you want to validate multiple controls. You can create ValidationSettings object using the GetValidationSettings method and passing every control you need to be validated. Then you can add the conditions you need to those validation settings. In your case you can create a custom condition for the length of the name and group condition with two RangeConditions for the Age requirements. Also you can set the isRequired property of these validation settings to true and it won’t allow empty values.
I have attached a sample demonstrating these suggestions.
Please let me know if you have any additional questions.