Validation shows me the errors in english and not in spanish

Last post 01-05-2009 4:34 PM by luisevalencia. 7 replies.

Sort Posts:

  • Validation shows me the errors in english and not in spanish

    12-29-2008, 5:32 PM
    • Participant
      976 point Participant
    • luisevalencia
    • Member since 08-11-2005, 9:28 AM
    • Medellin
    • Posts 258

    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

     

     


     

    MCPD ENTERPRISE APPLICATION DEVELOPER
  • Re: Validation shows me the errors in english and not in spanish

    12-29-2008, 7:06 PM
    • Contributor
      5,552 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 910
    • AspNetTeam
      Moderator

    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.

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
  • Re: Validation shows me the errors in english and not in spanish

    12-29-2008, 7:15 PM
    Answer
    • Contributor
      5,686 point Contributor
    • davidebb
    • Member since 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 1,131
    • AspNetTeam

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

     

  • Re: Validation shows me the errors in english and not in spanish

    12-29-2008, 7:24 PM
    Answer
    • Star
      11,992 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,516
    • TrustedFriends-MVPs

    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. 

    luisevalencia:
    [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>.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.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Validation shows me the errors in english and not in spanish

    12-31-2008, 5:49 PM
    Answer
    • Contributor
      5,552 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 910
    • AspNetTeam
      Moderator

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

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
  • Re: Validation shows me the errors in english and not in spanish

    01-02-2009, 6:33 PM
    Answer
    • Contributor
      2,024 point Contributor
    • marcind
    • Member since 09-06-2007, 5:11 AM
    • Redmond, WA
    • Posts 410
    • AspNetTeam

    sjnaughton:

    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.

    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Read my blog
  • Re: Validation shows me the errors in english and not in spanish

    01-02-2009, 6:54 PM
    • Star
      11,992 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,516
    • TrustedFriends-MVPs

    Thanks Marcin Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Validation shows me the errors in english and not in spanish

    01-05-2009, 4:34 PM
    • Participant
      976 point Participant
    • luisevalencia
    • Member since 08-11-2005, 9:28 AM
    • Medellin
    • Posts 258

     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!

    MCPD ENTERPRISE APPLICATION DEVELOPER
Page 1 of 1 (8 items)