Hello, I want to validate some fields that are decimal and integer
I wrote this class.
[DisplayName("Areas de Evaluación")]
public class EvaluationAreaMetaData
{
[ScaffoldColumn(false)]
public object Id { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Nombre")]
[StringLength(50)]
public object Name { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Porcentaje")]
[DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]
[Range(1, 100, ErrorMessage = "El valor debe ser un entero positivo, menor que 100")]
[DisplayFormat(DataFormatString="{0:f}%", ApplyFormatInEditMode=false)]
public object Percentaje { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Tipo de Servicio")]
public object ServiceType { get; set; }
The images are too small to read. Can you re-post the images or reproduce the problem with NorthWind or AdventureWorksLT? People usually have this problem when a validation error occurs from the data model that they have not over-ridden in the partial class.
Here you can see the addition in BOLD ITALIC this will force the the CompareValidator to show thr correct error message, this will need to be done in all FieldTemplates that the message will be overridden for. The above is the
Integer_Edit.ascx.cs file.
Dynamic DataCustom FieldTemplates
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Hopefuly one of the team can confirm this or correct the place to place the error message to override the default message.
Hi, just confirming that currently the set up method for CompareValidator does not take DataTypeAttribute into account when setting the error message. This will be fixed in future releases.
luisevalenci...
Participant
976 Points
258 Posts
Validation shows me the errors in english and not in spanish
Dec 29, 2008 09:32 PM|LINK
Hello, I want to validate some fields that are decimal and integer
I wrote this class.
[DisplayName("Areas de Evaluación")]
public class EvaluationAreaMetaData
{
[ScaffoldColumn(false)]
public object Id { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Nombre")]
[StringLength(50)]
public object Name { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Porcentaje")]
[DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]
[Range(1, 100, ErrorMessage = "El valor debe ser un entero positivo, menor que 100")]
[DisplayFormat(DataFormatString="{0:f}%", ApplyFormatInEditMode=false)]
public object Percentaje { get; set; }
[Required(ErrorMessage = "Este campo es Requerido")]
[DisplayName("Tipo de Servicio")]
public object ServiceType { get; set; }
[DisplayName("Grupos")]
[HideColumnIn(PageTemplate.Edit, PageTemplate.Insert)]
public object AreaGroups { get; set; }
}
BUt the behavior of the generated page is very strange, sometimes it shows me the error in spanish as I wrote it
but some times it shows me the validation error in english
[URL=http://img156.imageshack.us/my.php?image=80220227ul9.jpg][IMG]http://img156.imageshack.us/img156/9631/80220227ul9.th.jpg[/IMG][/URL]
[URL=http://img167.imageshack.us/my.php?image=92124628ca1.jpg][IMG]http://img167.imageshack.us/img167/2348/92124628ca1.th.jpg[/IMG][/URL]
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Validation shows me the errors in english and not in spanish
Dec 29, 2008 11:06 PM|LINK
The images are too small to read. Can you re-post the images or reproduce the problem with NorthWind or AdventureWorksLT? People usually have this problem when a validation error occurs from the data model that they have not over-ridden in the partial class.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Validation shows me the errors in english and not in spanish
Dec 29, 2008 11:15 PM|LINK
Yes, I think there is a bug here. Here are some workarounds:
To change the default message for all fields, add the following line at the end of Page_Load in Integer_Edit.ascx.cs:
CompareValidator1.ErrorMessage = String.Format("El campo {0} debe ser un entero", Column.Name);To use a custom message from the model, try something like this (again at the end of Page_Load in Integer_Edit.ascx.cs):
thanks,
David
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: Validation shows me the errors in english and not in spanish
Dec 29, 2008 11:24 PM|LINK
I think the issue is, that if DataTypeAttribute is the correct place to set the error message for the data typp the it is not being picked up.
Hopefuly one of the team can confirm this or correct the place to place the error message to override the default message.
A work around could be to modify the appropriate field templates:
protected void Page_Load(object sender, EventArgs e) { TextBox1.ToolTip = Column.Description; SetUpValidator(RequiredFieldValidator1); SetUpValidator(CompareValidator1); SetUpValidator(RegularExpressionValidator1); SetUpValidator(RangeValidator1); SetUpValidator(DynamicValidator1); var dataType = Column.Attributes.OfType()<DataTypeAttribute>.FirstOrDefault(); if (dataType != null) CompareValidator1.ErrorMessage = dataType.ErrorMessage; }Here you can see the addition in BOLD ITALIC this will force the the CompareValidator to show thr correct error message, this will need to be done in all FieldTemplates that the message will be overridden for. The above is the Integer_Edit.ascx.cs file.
Dynamic Data Custom FieldTemplates
Always seeking an elegant solution.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Validation shows me the errors in english and not in spanish
Dec 31, 2008 09:49 PM|LINK
I blogged David's approach here. Note a bug in the forum software hides some of David's code. His code should display as
var dataTypeAttribute = MetadataAttributes.OfType<DataTypeAttribute>().SingleOrDefault();
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Validation shows me the errors in english and not in spanish
Jan 02, 2009 10:33 PM|LINK
Hi, just confirming that currently the set up method for CompareValidator does not take DataTypeAttribute into account when setting the error message. This will be fixed in future releases.
ASP.NET Team
@marcind
Blog
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: Validation shows me the errors in english and not in spanish
Jan 02, 2009 10:54 PM|LINK
Thanks Marcin [:D]
Dynamic Data
Always seeking an elegant solution.
luisevalenci...
Participant
976 Points
258 Posts
Re: Validation shows me the errors in english and not in spanish
Jan 05, 2009 08:34 PM|LINK
What I did, was to create a new field template, and that's it.
However I like more your solutions, but I needed it very urgently so thats why I did that!