x19
x19

Reputation: 8783

Data annotation validation for determining rage of data does not work

I want to restrict values of a column in database to range of data.

In this particular case, I want Size column restrict integer number between 1 to 4.

I've used [Range(1, 4)] for this goal.Size column has been created but can get any range of integer value! Also about client validation I can enter any range of integer value!

How can I solve this problem?

I've used ASP.NET MVC 5, Entity Framework 6.x, Sql Server 2008R2 and Code-First for creating database.

If it's necessary, your solution can be use by db-migration.

My model:

public class Tag : Entity, ITag
{
    [Range(1, 4)]
    public virtual int Size { get; set; }

    [Required]
    [StringLength(25)]
    public virtual string Title { get; set; }

    [StringLength(256)]
    public virtual string Description { get; set; }

    public virtual bool IsActive { get; set; }

    public virtual ISet<ArticleTag> ArticleTags { get; set; }

    public virtual ISet<ProjectTag> ProjectTags { get; set; }
}

a part of my View:

<div class="form-group">
        @Html.LabelFor(model => model.Size, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Size)
            @Html.ValidationMessageFor(model => model.Size)
        </div>
    </div>

Upvotes: 0

Views: 323

Answers (1)

Goca
Goca

Reputation: 1863

This is a known problem apparently there is a issue with a version of jquery-validation so you can try using the following versions:

Microsoft jQuery Unobtrusive Validation (at least 2.0.30116.0)
Microsoft jQuery Unobtrusive Ajax (at least 2.0.30116.0)
jQuery Validation 1.11.1

you can find similar questions here: client-side validation trips on DataAnnotation Range attribute

and another report about this here: https://github.com/jzaefferer/jquery-validation/issues/626

Upvotes: 1

Related Questions