Error creating custom ASPNET MVC validation ( Client Side )

Asked

Viewed 57 times

0

I’m having a problem creating a custom validation using dataannotations by aspnet mvc.

My Model:

    public class Usuario
    {

        public string Nome { get; set; }


        [Idade(18)]   
        public string Senha { get; set; }
     }

My Validation Class:

    public class Idade : ValidationAttribute
    {
        private readonly int _idade;
        public Idade(int idade)
        {
            _idade = idade;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            if(value != null)
            {
                var idadeInformada = _idade;

                if (idadeInformada <= 18) {

                    return new ValidationResult(null);

                }
                else
                {
                    return new ValidationResult("Você não possui idade suficiente para se cadastrar");
                }
            }

            return ValidationResult.Success;
        }
    }

The big problem is that this custom validation is not working, when I give a POST on the page it does not display the validation error, unlike when I place the validations "native" of dataannotations

1 answer

0

Your logic is a little wrong, you are not comparing the value with the declared filter and the condition is reversed.

public class Idade : ValidationAttribute
{
    private int _idadeMinima;
    public Idade(int idadeMinima)
    {
        _idadeMinima = idadeMinima;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        if (value != null)
        {
            var idadeInformada = int.Parse(value.ToString());

            if (idadeInformada < _idadeMinima)
            {
                return new ValidationResult("Você não possui idade suficiente para se cadastrar");
            }
        }

        return ValidationResult.Success;
     }
}

Just like you should prepare your view to display the error message and return the model to it after validation of the post in the controller.

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(model => model.Idade, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Idade, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Idade, "", new { @class = "text-danger" })
    </div>
</div>
  • really the logic was wrong, but the error persists, the message does not appear.

  • Is your view prepared for this? You are returning Viewmodel to it after the post?

  • giving a search further sinking saw that has to be created a jquery.unobtrusive, that’s right?

  • @William Succeeded?

  • In your way I still could not, I did a test extending the jquery validate and it worked more or less.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.