jgauffin / griffin.mvccontrib

A contribution project for ASP.NET MVC3
http://blog.gauffin.org/tag/griffin-mvccontrib/
GNU Lesser General Public License v3.0
83 stars 40 forks source link

IValidatableObject code not called #18

Closed izzeda closed 12 years ago

izzeda commented 12 years ago

Hi. I used your 'TestProject' sample code from the codeproject page and let the HomeViewModel implement IValidatableObject, to do some extra checks. For instance: object-level validation, or a call to the database. But the Validate() method of IValidatableObject is never called.

Is there a way to let the Validation method be called?

The viewmodel code:

public class HomeViewModel : IValidatableObject
{
    [Required()]
    [StringLength(40)]
    public string Name { get; set; }

    [Required, Compare("Name", ErrorMessage = "Ange messy")]
    public int Age { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        //this is never called
        var errors = new List<ValidationResult>();
        if (nameIsNotUniqueInDatabase(Name))
            errors.Add(new ValidationResult(LocalizedErrors.NameIsNotUnique));

        return errors;
    }

    private bool nameIsNotUniqueInDatabase(string name)
    {
        //call to the database...
        return false;
    }
}

Thanks! Anna

jgauffin commented 12 years ago

Fixed in the next commit