1

simple custom validation,

my model and custom validation:

public class Registration
{
    [Required(ErrorMessage = "Date of Birth is required")]         
    [AgeV(18,ErrorMessage="You are not old enough to register")]
    public DateTime DateOfBirth { set; get; }
}

public class AgeVAttribute : ValidationAttribute
{
    private int _maxAge;

    public AgeVAttribute(int maxAge)
    {
        _maxAge = maxAge;
    }

    public override bool IsValid(object value)
    {
        return false;       <--- **this never gets executed.... what am I missing?**
    } 
}

(Please see the inline comment above)

view:

@using (Html.BeginForm()) {
@Html.ValidationSummary("Errors")
<fieldset>
    <legend>Registration</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.DateOfBirth)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DateOfBirth)    
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Benk
  • 1,284
  • 6
  • 33
  • 64
  • 1
    What does your controller that receives the model look like? – Brian Cauthon Apr 13 '12 at 16:09
  • 1
    I have tried your code in a blank MVC project and the call to IsValid happens as it should. – Iridio Apr 13 '12 at 16:10
  • Would you provide the 'action' method where the model of type 'Registration' is being used? I have tested your code, it's working on server side. As your 'AgeVAttribute' is not implementing IClientValidatable, so client-side validation is off. – Kibria Apr 13 '12 at 16:33

1 Answers1

2

Can't repro.

Model:

public class Registration
{
    [Required(ErrorMessage = "Date of Birth is required")]
    [AgeV(18, ErrorMessage = "You are not old enough to register")]
    public DateTime DateOfBirth { set; get; }
}

public class AgeVAttribute : ValidationAttribute
{
    private int _maxAge;

    public AgeVAttribute(int maxAge)
    {
        _maxAge = maxAge;
    }

    public override bool IsValid(object value)
    {
        return false;
    }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Registration
        {
            DateOfBirth = DateTime.Now.AddYears(-10)
        });
    }

    [HttpPost]
    public ActionResult Index(Registration model)
    {
        return View(model);
    }
}

View:

@model Registration

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary("Errors")
    <fieldset>
        <legend>Registration</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.DateOfBirth)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateOfBirth)    
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

The IsValid method is always hit when the form is submitted. Also notice that I haven't enabled client side validation because I didn't include the jquery.validate.js and the jquery.validate.unobtrusive.js scripts. If you have included them and there's an error chances are that client side validation will prevent your form from even being submitted to the server in which case it would be normal for the IsValid method not being invoked.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thx Darin, once I placed [HttpPost] public ActionResult Index(Registration model) { return View(model); } the IsValid method ran. If it is not difficult, could you please show me how would i do the check using client side validation, instead of doing serverside thanks for your help – Benk Apr 13 '12 at 16:21
  • You could have your custom validation attribute implement the `IClientValidatable` interface and then register a custom adapter which will represent a javascript function that you need to write and replicate the same validation logic you have on the server. Here's an example: http://stackoverflow.com/a/4747466/29407 – Darin Dimitrov Apr 13 '12 at 16:27
  • Hi Darin, I have checked the example you posted, everything is clear until I get to the last part (define the custom adapter) could you please show me how would you implement the age restriction (custom adapter), I look at the example I see.test() method, not sure what is doing.. if you could explain me that would be awesome thanks again. – Benk Apr 13 '12 at 16:54