1

I have the following sittuation:

    [Required(ErrorMessage = "Campo obrigatório!")]
    [MaxLength(10, ErrorMessage = "Tamanho excedido!")]
    [CustomDataAnnnotations.EhTelefoneViciado(ErrorMessage="Teste de validacao")]           
    public string NumeroTelefone1 { get; set; }

    [CustomDataAnnnotations.ValidarDataAtual(ErrorMessage = "Data futura!")]
    [Required(ErrorMessage = "Campo obrigatório!")]
    [Display(Name = "Data Ocorrido *")]
    public string DataOcorridos { get; set; }

This, generate the following HTML:

 <span class="field-validation-error erro" data-valmsg-for="NumeroTelefone1" data-valmsg-replace="true" title="Teste de validacao"></span>

 <span class="field-validation-error erro" data-valmsg-for="DataOcorridos" data-valmsg-replace="true">Data futura!</span>

Why on first scenario the message is in the TITLE of the SPAN and on the second IN the SPAN ? What i have to do, to generate like the second one?

I allready referenced the .js :

@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")

@{Html.EnableClientValidation(true);}
@{Html.EnableUnobtrusiveJavaScript(true);}

Help me, please.

EDIT: Below are the CustomDataAnnotation:

  public class CustomDataAnnnotations
{
    public class ValidarDataAtualAttribute : ValidationAttribute
    {
        public ValidarDataAtualAttribute()
        {
        }

        public override bool IsValid(object value)
        {
            //var dt = (DateTime)value;
            var dt = DateTime.Parse(value.ToString());
            if (dt <= DateTime.Now)
            {
                return true;
            }
            return false;
        }

    }

    public class EhTelefoneViciadoAttribute : ValidationAttribute
    {
        public EhTelefoneViciadoAttribute()
        {
        }

        public override bool IsValid(object value)
        {
            if (value == null) { return true; }

            var numeroTelefone = value.ToString();

            if (numeroTelefone.Length >= 8)
            {
                numeroTelefone = numeroTelefone.PadRight(8);

                String[] telefonesViciados = { "00000000", "11111111", "22222222", "33333333", "44444444", "55555555", "66666666", "77777777", "88888888", "99999999", "21212121", "10101010", "12121212", "23232323", "65656565", "45454545", "56565656", "78787878", "89898989" };

                if (telefonesViciados.Contains(numeroTelefone))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }


    }
}

EDIT02: The HTML:

   @Html.EditorFor(model => model.NumeroTelefone1, new { htmlAttributes = new { @class = "border-corner", @onkeydown = "return ValidateNumber(event);" } })
   @Html.ValidationMessageFor(model => model.NumeroTelefone1, "", new { @class = "erro" }

EDIT03: The validation error ( The label "TESTE" has to be on the side of the red cross)

Brazao
  • 19
  • 4
  • Assuming you are using HTML helpers in your view, what is the exact code you are calling? How are you rendering your model? – Jason Sep 21 '15 at 18:47
  • @Jason , edited with the infos... – Brazao Sep 21 '15 at 18:53
  • @Brazao Show where you actually call the HTML helpers in the view. Your validation attributes seem ok. – JB06 Sep 21 '15 at 19:39
  • @JB06 edit with the infos, i think the problem its the unobtrustive, this properties is in different views, but both view has the same declarations, am i missing something? – Brazao Sep 21 '15 at 19:42
  • @Brazao Try passing null instead of empty string into the second parameter of the ValidationMessageFor helper – JB06 Sep 21 '15 at 19:47
  • @JB06 same problem, I edited the original post with the img of the problem: – Brazao Sep 21 '15 at 19:51
  • Do you have any javascript errors in the dev tools console? – JB06 Sep 21 '15 at 20:28
  • There is nothing in the code you have posted that will cause that. The difference between the 2 code blocks is the order of the attributes so try swapping them to see if it makes any difference. You also have `@Scripts.Render("~/bundles/jqueryval")` which by default includes `jquery.validate.js` and `jquery.validate.unobtrusive.js` but then you add them again –  Sep 21 '15 at 23:39
  • @StephenMuecke Thks for your reply, unfortunately I made all the corrections and the problem still persist, i'm running out of options..; – Brazao Sep 22 '15 at 10:40
  • @Brazao, I have copies the code you have shown into a new project it does not generate `title="Teste de validacao"` in the html. It is something else you have that is causing the problem. –  Sep 22 '15 at 10:52
  • Is your EditorFor and ValidationMessageFor helpers for DataOcorridos declared in the same way as NumeroTelefone1? – JB06 Sep 22 '15 at 10:53
  • Exactly as you have shown (just changed `model => model.NumeroTelefone1` to `model => model.DataOcorridos`) and removed the attributes in both –  Sep 22 '15 at 10:56
  • The only thing that could possibly add the `title` attribute is javascript. The `ValidationMessageFor()` method does not add it. –  Sep 22 '15 at 10:58

2 Answers2

0

As i understand you are using some custom validation attributes (server-side validation only). So you expect the same validation to happen automatically on the client-side (with jquery validate).

First you need to add one more script which is jquery.unobtrusive-ajax.js. And if you want to have support on the client for your custom validation attributes take a look at this question in StackOverflow that i had made around 2 years ago. Hope it helps.

Community
  • 1
  • 1
Giorgos Manoltzas
  • 1,710
  • 5
  • 24
  • 34
  • Thx for your reply, what's bugging me, its that i already have a custom validation on another view, and works perfect ! Even if I use the same custom validation, the error persists... – Brazao Sep 21 '15 at 20:11
  • @Brazao unfortunately i cannot think of something else to help you. I am sorry. – Giorgos Manoltzas Sep 22 '15 at 11:30
0

thank you all for replying !

I found the error on a javascript function that was implemented on the code:

 $(document).ready(function () {
      $('.field-validation-error').each(function () {
           $(this).attr('title', $(this).html());
            $(this).html("")
        });
    });

This little bugga, it's the reason for my headache ! Anyway, thanks for the help !

Brazao
  • 19
  • 4