DataAnnotations CustomValidation not working

I've created a really simple code to test CustomValidations yet it is not working:

Validation

using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace UniversidadeCorporativa.Util
{
    public class CustomDDD : ValidationAttribute
    {
    private Regex _regex = new Regex(@"^[1-9]{2}$");

    public override bool IsValid(object value)
    {

        if (_regex.IsMatch(value.ToString()))
        {
            return true;
        }

        return false;
    }
}

ViewModel

using UniversidadeCorporativa.Util;
using System.ComponentModel.DataAnnotations;
namespace UniversidadeCorporativa.ViewModels
{
    public class TesteViewModel
    {
        public TesteViewModel()
        { }

        [Required]
        [CustomDDD]
        [Display(Name = "DDD")]
        public int DDDCel { get; set; }

        [Required]
        [Display(Name = "Celular")]
        public int Celular { get; set; }
    }
}

Controller

using UniversidadeCorporativa.ViewModels;
public ActionResult Teste()
{
    return View();
}

[HttpPost]
public ActionResult Teste(TesteViewModel model)
{
    try
    {
        return RedirectToAction("Teste");
    }
    catch
    {
        return View();
    }
}

View

@model UniversidadeCorporativa.ViewModels.TesteViewModel

@{
    ViewBag.Title = "Teste";
}

<h2>Teste</h2>


@using (Html.BeginForm("Teste", "Universidade", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    @*@Html.ValidationSummary("", new { @class = "text-danger" })*@
    <div class="form-group">
        @Html.LabelFor(m => m.Celular, new { @class = "col-md-2 control-label" })
        <div class="col-md-2">
            @Html.TextBoxFor(m => m.DDDCel, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.DDDCel)
        </div>
        <div class="col-md-8">
            @Html.TextBoxFor(m => m.Celular, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Celular)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Teste" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Nothing is validated when the input is submited and i have no ideia why. Can someone help?

Upvotes: 0

Views: 170

Answers (1)

Barry Gallagher
Barry Gallagher

Reputation: 6246

When you add a custom validation, you will have to test for it server side by checking ModelState.IsValid on your Teste action:

[HttpPost]
public ActionResult Teste(TesteViewModel model)
{
    try
    {
        if(ModelState.IsValid)
        {
            // your model is valid!
        }

        return RedirectToAction("Teste");
    }
    catch
    {
        return View();
    }
}

As it stands, a CustomValidation attribute will not be automatically hooked into jQuery's unobtrusive validation library. So, it won't work client-side until you create a custom jQuery validation rule. See this blog post for reference.

Upvotes: 2

Related Questions