 |
|
|
 |
|
 |
hi,
i have a problem with this control. on my page, i use validationgroups.
so the normal validationsummary has a validationgroup too. in case of this,
the custom errors will not be displayed. has anyone a idea for this problem?
i want to set a validationgroup to the customsummary, but it didn't work.
thanks for answers
greets joerg
|
|
|
|
 |
|
 |
I may have found a solution to this. I haven't throughly tested it, but prelimenary tests are successful.
[ToolboxData("<{0}:MyValidationSummary runat="server"></{0}:MyValidationSummary>")]
public class MyValidationSummary : ValidationSummary
{
public void AddErrorMessage(string message)
{
this.Page.Validators.Add(new DummyValidator(message, this.ValidationGroup));
}
public void AddErrorMessage(string message, string validationGroup)
{
this.Page.Validators.Add(new DummyValidator(message, validationGroup));
}
public void AddErrorMessage(IEnumerable<string> messages)
{
new List<string>(messages).ForEach(delegate(string message) { this.Page.Validators.Add(new DummyValidator(message, this.ValidationGroup)); });
}
public void AddErrorMessage(IEnumerable<string> messages, string validationGroup)
{
new List<string>(messages).ForEach(delegate(string message) { this.Page.Validators.Add(new DummyValidator(message, validationGroup)); });
}
private class DummyValidator : CustomValidator
{
public DummyValidator(string errorMessage, string validationGroup)
{
this.ErrorMessage = errorMessage;
this.ValidationGroup = validationGroup;
this.IsValid = string.IsNullOrEmpty(this.ErrorMessage);
}
protected override void OnUnload(EventArgs e)
{
if (this.Page != null)
{
this.Page.Validators.Remove(this);
}
base.OnUnload(e);
}
}
}
I made some minor modifications to the code above.
1. added overrides to insert with or without validation group.
2. added AddErrorMessages to insert a collection of error messages.
3. DummyValidator inherits from CustomValidator to access ValidationGroup functionality.
4. DummyValidator is valid if the error message is not null or empty.
5. DummyValidator is an internal class for MyValidationSummary.
Jason Meckley
Programmer
Specialty Bakers, Inc.
|
|
|
|
 |
|
 |
thanks a lot!
this is working well!
|
|
|
|
 |
|
 |
seems to be the problem with the "page" variable.
scroll down to the error entry which named "error". i think this will
solve your problem.
|
|
|
|
 |
|
 |
sorry, i mean the entry in this forum from rizob. he made an entry called "error".
|
|
|
|
 |
|
 |
here are my changes.
to show the summary:
ValidationCustomSummary CustomSummary = new ValidationCustomSummary();
CustomSummary.AddErrorMessage(GetGlobalResourceObject("Globals", "passwordChanged").ToString(), "validateOK", this.Page);
ValidationCustomSummary.cs:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
namespace myControls
{
[ToolboxData("<{0}:MyValidationSummary runat="server"></{0}:MyValidationSummary>")]
public class ValidationCustomSummary : ValidationSummary
{
public void AddErrorMessage(string message, Page pageRef)
{
pageRef.Validators.Add(new DummyValidator(message, this.ValidationGroup));
}
public void AddErrorMessage(string message, string validationGroup, Page pageRef)
{
pageRef.Validators.Add(new DummyValidator(message, validationGroup));
}
public void AddErrorMessage(IEnumerable<string> messages, Page pageRef)
{
new List<string>(messages).ForEach(delegate(string message) { pageRef.Validators.Add(new DummyValidator(message, this.ValidationGroup)); });
}
public void AddErrorMessage(IEnumerable<string> messages, string validationGroup, Page pageRef)
{
new List<string>(messages).ForEach(delegate(string message) { pageRef.Validators.Add(new DummyValidator(message, validationGroup)); });
}
public DummyValidator(string errorMessage, string validationGroup)
{
this.ErrorMessage = errorMessage;
this.ValidationGroup = validationGroup;
this.IsValid = string.IsNullOrEmpty(this.ErrorMessage);
}
protected override void OnUnload(EventArgs e)
{
if (this.Page != null)
{
this.Page.Validators.Remove(this);
}
base.OnUnload(e);
}
}
}
}
|
|
|
|
 |
|
|
 |
|
 |
I've altered the dummy validator class to solve this problem.
Instead of implementing the Ivalidator interface just inherit the basevalidator class. This way you can set the validation group property for the dummyvalidator.
Usage:
<code>// Create new dummy validator
DummyValidator dv = new DummyValidator("Something went wrong");
// Set validationgroup
dv.ValidationGroup = "LeadValidators";
// Add validator to collection
Page.Validators.Add(dv);</code>
The class:
<code>public class DummyValidator : BaseValidator
{
public DummyValidator(string msg)
{
ErrorMessage = msg;
this.IsValid = false;
}
protected override bool EvaluateIsValid()
{
return false;
}
} // End of class DummyValidator
|
|
|
|
 |
|
 |
Isn't the best practice to ensure the integrity of the data first before the trip to the server, and also before any erroneous data can possibly be entered ?
Take for example a field that must be alpha or numeric only -
I can send "123456" or "ABCDEF" and both would be accepted - if for example the data type on the database was char(6)
One might ask why would anyone store Numeric only data as char and not as integer - well b/c one might have data with leading zeroes and integer types remove the leading zeroes [01234] is not the same as [1234].
While you certainly can use the sqlserver to provide the summary for the validation the way you have done it - I think that in the long run you will end up in a bad scenario.
|
|
|
|
 |
|
 |
stixoffire wrote: Isn't the best practice to ensure the integrity of the data first before the trip to the server, and also before any erroneous data can possibly be entered ?
yes it is good practice to validate before postback. however, the trade off is code duplication and reuseablitiy. If I place my data validation into my Domain Model, then my validation is in one place no matter where I use the object in the GUI.
if all validation is done on the GUI, how would a shopping cart be validated. you would need to validation the cart was not empty, you would also need to validate the payment, customer, etc. this is better suited for the Domain Model rather than the GUI.
stixoffire wrote: One might ask why would anyone store Numeric only data as char and not as integer - well b/c one might have data with leading zeroes and integer types remove the leading zeroes [01234] is not the same as [1234].
certainly true. although the other argument would be storing the leading zeros is extransious data. Rather this could be a formatting issue which should be dealt with in the either the Domain Model or presentaion layer.
Jason Meckley
Programmer
Specialty Bakers, Inc.
|
|
|
|
 |
|
 |
| Data with Leading zeroes
"Rather this could be a formatting issue which should be dealt with in the either the Domain Model or presentaion layer."
That would be nice if all data was organized that way - but sometimes when different manufacturers merge with disparate item numbering schemes or schemes that could represent the same item - such as a 4545 being one item and a 04545 being another (sure I would like the easy way - but some People want to make things difficult - (maybe they think difficult is JOB Security)..
I agree with your arguments to the point of where and how the validation is implemented - client side vs. server side - on the client obviously AJAX / Java is good way to go..no postbacks etc.. - I like to create custom controls to do things - so for me - the validation in the custom control (programmability built in of course ) - yes a lot more duplication of effort per control but not per use..lots of pros and cons - could say my controls are bloated..
Thanks for the response..
|
|
|
|
 |
|
 |
Hi
Thanks for this, it's very useful but it didn't work with me the page is always empty.
this is the code
protected void registerCommand_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
Membership.CreateUser(usernameTextBox.Text, passwordTextBox.Text, EMailTextBox.Text);
}
catch (Exception E)
{
// Set Dynamic Error
ValidationSummary1.AddErrorMessage(E.Message);
// Redirect
}
}
}
Thanks
|
|
|
|
 |
|
 |
Instead adding yet another ControlEx to my codelib, i've looked at your code and used it in a different way.
I haven't inherited from ValidationSummary, but instead i've just implemented a class that adds a message to the page this.Page.Validators. Doing so I can use the special instance(or static class) when i need to without changing the validationcontrols on my page.
Thanks for the Tip
Simplify is the Max
|
|
|
|
 |
|
 |
Hi,
Can you share with us a sample page. I prefer your approach however I can't make it work.
Many thanks
|
|
|
|
 |
|
 |
I would like to see that posted somewhere ??
|
|
|
|
 |
|
 |
This article helped my thinking alot and provided a solution for my problem, so I thank thee.
I did have trouble with the object reference issue as mentioned by rizob, and his solution worked aswell.
To futher my implementation, i created a new instance of the control on the code-behind page where a validation summary control were already created on the aspx page.
So whenever I get an data driven error/exception, i just pass the error message into the custom validator and the exception is neatly handled!
ah and an example page would have helped aswell
|
|
|
|
 |
|
 |
I think that it's a big minus that the message box is not functional with this implementation.
As far as I've seen the dummy validators are not rendered on the page.
If the message box is a client script (must be) than it won't find these dummy validators... (probably this is what happens there)
|
|
|
|
 |
|
 |
It took me awhile to figure out how to use the ValidationSummary. I kept trying to instantiate one on the page. It finally occured to me to add one to the page dynamically. I think a sample showing this would be useful.
Randall Moore
http://www.IssueView.Com
|
|
|
|
 |
|
 |
I still don't understand how to use this control...
|
|
|
|
 |
|
 |
Hi.
I have created a class using the provided code. I get the error 'object reference not set to an instance of an object'. It seems to be occurring on this line.
this.Page.Validators.Add(new DummyValidator(msg));
I created an instance to the DummyValidator and commented out the above line and got no error. So, it is clearly not referencing the 'Page' correctly. Any ideas on where I am going wrong.
Thanks.
rizob
|
|
|
|
 |
|
 |
solved. here's what i did in case anyone else encounters this problem. in my calling page i created a reference to the page. 'system.web.ui.page pageRef = this.page;' add this to the class method. 'AddErrorMsg(string msg, system.web.ui.page pageRef)' then set a variable in the class. 'system.web.ui.page mypageRef = pageRef' finally use the variable to 'mypageRef.validators.add(DummyValidator(msg));'
code works great now. thanks.
|
|
|
|
 |
|
 |
solved. here's what i did in case anyone else encounters this problem. in my calling page i created a reference to the page. 'system.web.ui.page pageRef = this.page;' add this to the class method. 'AddErrorMsg(string msg, system.web.ui.page pageRef)' then set a variable in the class. 'system.web.ui.page mypageRef = pageRef' finally use the variable to 'mypageRef.validators.add(new DummyValidator(msg));'
code works great now. thanks.
|
|
|
|
 |
|
 |
...once I've added it to the summary, it seems to prevent postback when all other validator controls are "happy". And since my logic as to whether to display the message is here I'm stuck
When I invalidate any other validation control I can then postback after correcting it
|
|
|
|
 |
|
 |
Is it possible that IsValid is not set to true?
|
|
|
|
 |