Question

I understood how to create custom validation attributes. In fact, the Validate method is being execute when I use the Seed method to pre-populate the database, and if it fails it throws an exception. However, the validation is not working on the Create Form of the entity.

Do I have to change something to the HTML (Razor Form)?

It just lets me add items that fail the validation.

Code here:

namespace Data.Model
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class YearsValidationAttribute : ValidationAttribute
    {
        // Internal field to hold the min value.
        readonly int _years;

        public int Years
        {
            get { return _years; }
        }

        public YearsValidationAttribute(int years)
        {
            _years = years;
        }


        public override bool IsValid(object value)
        {
            var years = (int)value;
            bool result = true;
            if (this.Years != null)
            {
                result = Years >= years;
            }
            return result;
        }



        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture,
              ErrorMessageString, name, Years);
        }
    }
}


public class Position
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
        public int PositionID { get; set; }

        [Required(ErrorMessage = "Position name is required.")]
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
        [Display(Name = "Position name")]              
        public string name { get; set; }

        [Required(ErrorMessage = "Number of years is required")] 
        [Display(Name = "Number of years")]
        [YearsValidationAttribute(5, ErrorMessage = "{0} value must be greater than {1} years.")]        
        public int yearsExperienceRequired { get; set; }

        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }



@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Position</legend>

        @Html.HiddenFor(model => model.PositionID)

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.yearsExperienceRequired)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.yearsExperienceRequired)
            @Html.ValidationMessageFor(model => model.yearsExperienceRequired)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
Was it helpful?

Solution

That code you have won't give you a client validator, only the server side. But that is a good starting point. What you need to do in your action method on the server is to check if the model is valid like such:

public ActionResult YourAction(YourModel model)
{
    if(ModelState.IsValid)
    {
        // Do your save
    }
    else
    {
        // Do your other stuff
    }
}

If you want client side validation as well you can use the resources here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

You can also try to use the Remote validation attribute: http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top