locked
Validation shows me the errors in english and not in spanish RRS feed

  • Question

  • User1204604062 posted

    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]

     

    Monday, December 29, 2008 5:32 PM

Answers

  • User1641955678 posted

    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):

        var dataTypeAttribute = MetadataAttributes.OfType<DATATYPEATTRIBUTE>().SingleOrDefault();
        if (dataTypeAttribute != null) {
            CompareValidator1.ErrorMessage = dataTypeAttribute.FormatErrorMessage(Column.Name);
        }
    
     
    I will open a bug to get this fixed in the next release.

    thanks,
    David

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2008 7:15 PM
  • User-330204900 posted

    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. 

    [DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]

    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>()<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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2008 7:24 PM
  • User-1005219520 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 31, 2008 5:49 PM
  • User-797310475 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2009 6:33 PM

All replies

  • User-1005219520 posted

    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.

    Monday, December 29, 2008 7:06 PM
  • User1641955678 posted

    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):

        var dataTypeAttribute = MetadataAttributes.OfType<DATATYPEATTRIBUTE>().SingleOrDefault();
        if (dataTypeAttribute != null) {
            CompareValidator1.ErrorMessage = dataTypeAttribute.FormatErrorMessage(Column.Name);
        }
    
     
    I will open a bug to get this fixed in the next release.

    thanks,
    David

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2008 7:15 PM
  • User-330204900 posted

    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. 

    [DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]

    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>()<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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2008 7:24 PM
  • User-1005219520 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 31, 2008 5:49 PM
  • User-797310475 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2009 6:33 PM
  • User-330204900 posted

    Thanks Marcin [:D]

    Friday, January 2, 2009 6:54 PM
  • User1204604062 posted

     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!

    Monday, January 5, 2009 4:34 PM