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>
}
3
  • 1
    What does your controller that receives the model look like? Commented Apr 13, 2012 at 16:09
  • 1
    I have tried your code in a blank MVC project and the call to IsValid happens as it should. Commented Apr 13, 2012 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. Commented Apr 13, 2012 at 16:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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: stackoverflow.com/a/4747466/29407
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.