View Full Version : Validation of data prior to unbinding a control
Alistair
05-04-2005, 07:02 AM
Hi
I've been experimenting with using the Spring.Web bidirectional data binding and wondering how other people are getting around data conversion errors.
For example, if I bind a TextBox to an INT property, and the user submits a value that is not an INT then an "Input string was not in a correct format" error is returned.
I was hoping there would be a means to perform validation gracefully. I've used Tapestry a fair bit and its bidirectional databinding and it allows for:
A "validator" class to be specified along with the binding so you can control exactly what happens in the case of a conversion error and no exception is ever thrown
To defer validation/conversion in the case the the postback doesn't represent a logical submit of data but has been triggered for some other reason (such as to filter a drop down list)
What's the best approach to dealing with these issues in Spring.Web?
Thanks
Alistair
Alistair
05-05-2005, 02:46 AM
I think I will work around this issue by using a "strongly typed" user control that can perform the necessary validation and logic prior to returning the value to the Spring binding classes.
If anyone has any other approaches I'd be interested to hear them.
Thanks
Alistair
Aleks Seovic
05-07-2005, 08:40 AM
Have you tried using standard .Net validator mechanism?
Validators execute before data binding code during postback, so you should be able to catch any errors in the format before DataBinder tries to perform data conversion.
- Aleks
Alistair
05-08-2005, 11:48 PM
Thanks - as far as I know there is no data type validator although I could use the regular expression validator for this purpose.
Ted Husted
05-09-2005, 03:44 PM
You can also write your own custom validators using the .NET framework.
Alistair
05-09-2005, 11:35 PM
Thanks. I've been looking into that and will probably go down that path but in the process will break the .Net pattern of having a validator work with a single field. I'm considering writing a validator that works across all fields in the form, the problem is that the validator summary control will only display one message per validator, so looks like I need my own version of the summary control too....
Does anyone use the validators as is in complex applications? It seems they're design only for very simple "shopping cart" type applications...
Ted Husted
05-10-2005, 12:05 AM
In real-life, AFAIK, you can't do all of the validation ASP.NET style, since there are validations that depend on business logic. So, what happens is that people try to do "domain" or "prima facia" validation on the web layer, and then do the rest of the validation on the business layer. It's the difference between "password needs to be at least 8 characters long" and "username or password is not valid". Often, people will put the latter type of message in a separate message area.
On the Java side, there's integration with the Apache Jakarta Commons Validator, which permits multiple field validations and such.
http://opensource.atlassian.com/confluence/spring/display/DISC/Multiple+Validators+per+Controller?showComments=tr ue
-Ted.
Alistair
05-10-2005, 12:10 AM
Ted, thanks - I understand what needs to be done on the presentation layer as opposed to within the service layer, and am quite familiar with Struts and Tapestry on the Java side.
What I am really wondering is whether when building a web form in ASP.net (with say 40+ fields) people really use the ASP.Net validator controls... it seems so cumbersome. With 40 - 80 web forms and possibly averaging 1 - 2 validators per field it seems unworkable. Additionally, it doesn't seem that the validators are useable in repeating controls, only for controls that exist as single instances on the page. Does that make sense?
Alistair
05-10-2005, 04:04 AM
It seems the CompareValidator provides data type checking as one of its options - something I didn't know before. Its a shame the whole validator framework in ASP.net is such a cludge but it seems its the only option short of replacing it completely with custom code/controls.
sebastijanp
06-08-2005, 10:38 AM
I have a normal textBox and use bidirectional bind on int data. I can not validate data cause I get exception. What to do? There is no way to get around this?
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator id="CustomValidatorTextBox1" runat="server"
ControlToValidate="TextBox1"
OnServerValidate="ServerValidate"
Display="Static"
Font-Name="Verdana" Width="200px">Wrong format!
</asp:CustomValidator>
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 211: if (converter != null && converter.CanConvertFrom(sourceExp.ExpressionType) )
Line 212: {
Line 213: targetExp[0].SetValue(target, converter.ConvertFrom(obj));
Line 214: }
Line 215: else
Source File: F:\MyProjects\SpringNet\0.5.15\src\Spring\Spring.W eb\Web\DataBinding\BindingAttribute.cs Line: 213
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +0
System.Int32.Parse(String s, NumberStyles style, IFormatProvider provider) +37
System.ComponentModel.Int32Converter.FromString(St ring value, NumberFormatInfo formatInfo)
System.ComponentModel.BaseNumberConverter.ConvertF rom(ITypeDescriptorContext context, CultureInfo culture, Object value)
[Exception: sdyf is not a valid value for Int32.]
System.ComponentModel.BaseNumberConverter.ConvertF rom(ITypeDescriptorContext context, CultureInfo culture, Object value)
System.ComponentModel.TypeConverter.ConvertFrom(Ob ject value)
Spring.Web.DataBinding.BindingAttribute.Unbind(Obj ect source, Object target) in F:\MyProjects\SpringNet\0.5.15\src\Spring\Spring.W eb\Web\DataBinding\BindingAttribute.cs:213
[DataBindingException: Failed to unbind property 'Text' to target 'System.String[]'.]
Spring.Web.DataBinding.BindingAttribute.Unbind(Obj ect source, Object target) in F:\MyProjects\SpringNet\0.5.15\src\Spring\Spring.W eb\Web\DataBinding\BindingAttribute.cs:224
Spring.Web.DataBinding.DataBinder.UnbindData() in F:\MyProjects\SpringNet\0.5.15\src\Spring\Spring.W eb\Web\DataBinding\DataBinder.cs:161
Spring.Web.UI.Page.OnLoad(EventArgs e) in F:\MyProjects\SpringNet\0.5.15\src\Spring\Spring.W eb\Web\UI\Page.cs:149
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
Lp
Sebastijan
sebastijanp
06-08-2005, 11:16 AM
OK! I am sorry! My mistake. I resolved it with:
<asp:requiredfieldvalidator id="Requiredfieldvalidator2" runat="server"
Font-Names="Arial" ControlToValidate="TextBox1"
CssClass="spanCapnDarkRedErr" Display="Dynamic" Font-Name="Verdana" Width="250px">
* Količina je obvezno polje!</asp:requiredfieldvalidator>
<asp:CompareValidator id="comp1"
ControlToValidate="TextBox1" Type="Integer" Operator="DataTypeCheck"
runat="server" Display="Dynamic"
ErrorMessage = "You must enter an integer value." />
Thank anyway for future help:)
Lp
Sebastijan
Mark Pollack
06-11-2005, 09:30 PM
Hi,
There are the similar validation issues using WinForms. Ian Griffiths author of .NET Windows Forms in a Nutshell summarized the situation - see Win Forms Validation Broken (http://pluralsight.com/wiki/default.aspx/Craig/WinFormsValidationBroken.html).
Adding more advanced validation support in both realms would be very useful. How well do you think commons validator functionality meets your requirements? Generally speaking there are a fair amount of commons projects that would be nice to have in the .NET world and one wonders if Spring.NET is the proper home for these libraries. That being said, we already have core parts of commons-pool ported as well as Doug Lea's concurrency utilities.
Cheers,
Mark
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.