 |
|
|
 |
|
 |
Misleading title and tags
|
|
|
|
 |
|
 |
This article is about Web forms.
|
|
|
|
 |
|
|
 |
|
 |
This is web forms, not Windows Forms. Thanks for wasting my time. Please rename your article.
|
|
|
|
 |
|
 |
cjard wrote: This is web forms, not Windows Forms. Thanks for wasting my time. Please rename your article.
Well said, this is WEB forms, not win forms! thanks for wasting my time also
|
|
|
|
 |
|
 |
ya.. please change title..
|
|
|
|
 |
|
 |
One question:
Should the code of method 'Validate' of the if else not be swapped (because FocusChange mode checks validates now every control)?
Two suggestions:
1. Code to clear each errormessage, eg:
Class 'Validator':
public void Clear()
{
foreach (Control control in this.infos.Keys)
{
ValidationInfo info = this.infos[control];
info.Clear();
}
}
Class 'ValidationInfo':
public void Clear()
{
this.errorProvider.SetError(control, String.Empty);
}
2. A method to specifically Validate a control. Now all controls will be validated upon calling Validate.
public bool Validate(Control controlToValidate)
Nice article!
|
|
|
|
 |
|
 |
Thanks very much for this awsome contol,it provides all what i need.
|
|
|
|
 |
|
 |
Hi
Has anyone managed to get this to work the the DataGridView? That's my current challenge...
Daniel
|
|
|
|
 |
|
 |
does this validator differ from error provider? if yes what is the difference?
|
|
|
|
 |
|
 |
Your validator works excellent! It helped me a lot. Thanks again
|
|
|
|
 |
|
 |
Hi,
This is exactly what i need! Thank you!
Still one question:
How do i remove the icon when i'm removing the validation on a Control;
Little example:
if (enableGroupbox1)
{
// Enable grpBox1
this.grpBox1.Enable = true;
// Add validation to fields
validator1.SetType(txtFirstName, Itboy.Components.ValidationType.Required);
validator1.SetRequiredMessage(txtFirstName, "Firstname is required");
}
else
{
// Disable grpBox1
this.grpBox1.Enable = false;
// Remove validation
validator1.SeType(txtFirstName, Itboy.Components.ValidationType.None);
validator1.SetRequiredMessage(txtFirstName, null);
}
Now, when i disable the groupbox, and didn't fill in the Control txtFirstName correctly (when it was enabled), there is still the little icon.
Any idea's howto remove this?
Thanks in advance,
Grt Jeroen
|
|
|
|
 |
|
 |
Okay, this seems to be the #1 area I see application design errors (in my opinion). Long ago, especially with Web Apps, the validation had to be separate from the data object itself due the the nature of how web browsers communicated with data servers.
This is no longer the case, but the old design patterns refuse to leave. The UI, whether it is a web browser or a web form, should not know anything about validation... zip, zero, nada. Think about it, when a designer creates a data object (class), that class should set the validation rules about itself, not the textbox, or grid or whatever control is used to display the data.
You began the article with having to duplicate code, hence your validation control. But, you still must duplicate code for every form (browser or winform) that provides access to the data and your actual data object has a big gaping whole that allows bad data if you design a new form and miss "something" in the validation.
I suggest you read up on the IEditableObject, IPropertyChangeNotification, and IDataErrorInfo interfaces. Object classes should validate themselves and simply provide the information to the UI via these interfaces.
I do not have one line of validation code in the UI: zip, zero, nada on any form, yet the validation is robust. Furthermore, I create the validation for let's say 'Email Address', 'Credit Card Number', etc in ONE place in the business tier and the object itself simply calls IsValid(Email,textFromProperty, out message), if it fails the validation, then IDataErrorInfo is trigered for that property and the form displays the error without any validation code in the UI. Furthermore, since "rules" are consolidated in one location and the rule for, let's say 'Credit Card Number', changes, then I change it in ONE spot and I know every place that spawned from it(depends on it) updates without making any other "unecessary" changes to code.
My advice: never, ever place validation within the presentation tier - never... for the very same reasons you began the article with. Just because the old limitations have spawned bad habits that are still very popular does not mean they should be continued.
This is a perfect example of the "popular way" does not mean its the "right" way to go.
Having said all that, it was a nice article for what it was trying to do, but I personally would never validate data in this manner and I still rated your article high, it was done well.
Trevor
|
|
|
|
 |
|
 |
Hmmm.... a good theoretical response Trevor. I'm looking forward to seeing your article illustrating it practically
Peter Hancock
My blog is here
And they still ran faster and faster and faster, till they all just melted away, and there was nothing left but a great big pool of melted butter
"I ask candidates to create an object model of a chicken." -Bruce Eckel
|
|
|
|
 |
|
 |
A good example of how to keep validation in the business layer can be found in the Microsoft patterns and practices validation block, it is expert level stuff and takes a while to be able to use and understand but it is the way to go if you are looking to build enterprise level applications
|
|
|
|
 |
|
 |
Thanks for the info. about those interfaces Trevor, I am going to be looking into using these for the project I'm working on
One note is that the 'IPropertyChangeNotification' interface is actually 'INotifyPropertyChanged'. And for anyone else looking for them, they're in the System.ComponentModel namespace.
|
|
|
|
 |
|
 |
Excellent point. Provide a clean, practical POC article and you've have a winner.
"We have a proven antidote to war: free trade." ~ Paul Zane Pilzer
|
|
|
|
 |
|
|
 |
|
 |
Excellent work,
I noticed this control work only on a Form. Is there a way to extened it to be dropped in a UserControl, Basically my user control has all the window elements so I will need to drop the Validatior in the user control
Thank You
Ali
|
|
|
|
 |
|
 |
I would suggest it to. UserControl support is missing at this time to get it fully useful.
Thanks.
|
|
|
|
 |
|
 |
Great code! You can alter this project to work with User Controls by doing the following:
//comment out or delete code in /* */ and add the following to the Validator.cs as shown in the sample.
public override void InitializeNewComponent(System.Collections.IDictionary defaultValues)
{
components = new System.ComponentModel.Container();
/* // Set container form as validator's form.
((Validator)this.Component).Form = (Form)this.ParentComponent;
base.InitializeNewComponent(defaultValues);*/
}
//dont forget to declare "components":
private System.ComponentModel.Container components = null;
Thanks to the Noogen project for help in this (search this site for Noogen)
|
|
|
|
 |
|
 |
I see that you have the same problem with the error icons as I have. I haven't found anywhere on the internet answer how to make the error icons round. Do you see the ugly artifacts that are around the icon? It should be round. When you make the icon blink and after some time they stop blinking, they looks nice and round. But try to catch the window and move it off-screen so the icons are not visible and then move it back - and see? The icons get ugly borders... The same problem is when you disable blinking. It seems like problem with rendering alpha channel. Have you anyone found a workaround to this behaviour?
|
|
|
|
 |
|
 |
I think they were designed to look like a STOP sign - 8 sided.
Probably not a good idea from a global perspective...
|
|
|
|
 |
|
 |
this is a very cool tool. I was about to write something similar, but you where faster. It works great so far.
I noticed 1 thing: if i write something wrong into each, the blinking of all of them can be annoying (as they all blink at different times), but hey.. i just disabled blinking (fits more my style anyways).
I changed one other thing:
string oldmessage = this.errorProvider.GetError(control);
if (string.IsNullOrEmpty(oldmessage) != string.IsNullOrEmpty(message))
{
int shift = string.IsNullOrEmpty(oldmessage) ? -this.errorProvider.Icon.Width : +this.errorProvider.Icon.Width;
control.Width += shift;
}
this.errorProvider.SetError(control, message);
that way, the actual sign fits into the rectangle of the invalid control.
thanks, this code will definitely be put to use
|
|
|
|
 |