Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String MinLength and MaxLength validation don't work (asp.net mvc)

I have this class

using System.ComponentModel.DataAnnotations;
using Argussoft.BI.DAL.Domain.Users;

namespace Argussoft.BI.DAL.DTOs.UserDTOs
{
    public class CreateUserDto
    {
        [Required(ErrorMessage = "Введите логин")]
        [MaxLength(User.EmailLength, ErrorMessage = "Максимальная длина логина 40 символов")]
        [RegularExpression(User.NameRegularExpression, ErrorMessage = "Логин может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Введите Email")]
        [MaxLength(User.EmailLength, ErrorMessage = "Максимальная длина адреса электронной почты 100 символов")]
        [RegularExpression(User.EmailRegularExpression, ErrorMessage = "Введите корректный адрес электронной почты")]
        public virtual string Email { get; set; }

        [Required(ErrorMessage = "Введите имя пользователя")]
        [MaxLength(User.FullNameLength, ErrorMessage = "Максимальная длина имени пользователя 100 символов")]
        [RegularExpression(User.NameRegularExpression, ErrorMessage = "Имя пользователя может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public virtual string FullName { get; set; }

        public virtual int Role { get; set; }

        [RegularExpression(User.PhoneRegularExpression, ErrorMessage = "Введите корректный номер телефона")]
        public virtual string Phone { get; set; }

        public virtual int Status { get; set; }

        [Required(ErrorMessage = "Введите пароль")]
        [MinLength(User.PasswordMinLength, ErrorMessage = "Минимальная длина пароля 5 символов")]
        [MaxLength(User.PasswordMaxLength, ErrorMessage = "Максимальная длина пароля 20 символов")]
        [RegularExpression(User.PasswordRegularExpression, ErrorMessage = "Пароль может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public virtual string Password { get; set; }
    }
}

and this form

@model Argussoft.BI.DAL.DTOs.UserDTOs.CreateUserDto

@using (Ajax.BeginForm("CreateUser", "User", new AjaxOptions { OnSuccess = "onSuccessCreateUser" }, new { id = "dialog_form", @class = "form-horizontal" }))
{
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4>Добавить пользователя</h4>
    </div>
    <div class="modal-body">

        <!-- Name -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Name)">Логин</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Name, new { @class = "span3", id = "input_name" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>
        </div>

        <!-- Email -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Email)">Email</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Email, new { @class = "span3", id = "input_email" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Email)
                </div>
            </div>
        </div>

        <!-- FullName -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.FullName)">Имя пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.FullName, new { @class = "span3", id = "input_full_name" })
                    <br />
                    @Html.ValidationMessageFor(model => model.FullName)
                </div>
            </div>
        </div>

        <!-- Role -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Role)">Роль пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.DropDownList("Role", (SelectList)ViewBag.Roles,new{id ="input_role"})
                    <br />
                    @Html.ValidationMessageFor(model => model.Role)
                </div>
            </div>
        </div>

        <!-- Phone -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Phone)">Контактный телефон</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Phone, new { @class = "span3", id = "input_phone" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Phone)
                </div>
            </div>
        </div>

        <!-- Status -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Status)">Статус пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.DropDownList("Status", (SelectList)ViewBag.UserStatuses,new{id ="input_status"})
                    <br />
                    @Html.ValidationMessageFor(model => model.Status)
                </div>
            </div>
        </div>

        <!-- Password -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Password)">Пароль пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Password, new { @class = "span3", id = "input_password" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>
        </div>

    </div>
    <div class="modal-footer">
        <a href="@Url.Action("UserIndex", "User")" class="btn">Отменить</a>
        <button type="submit" id="save_button" class="btn btn-primary">Сохранить</button>
    </div>
}

User class:

public class User : BaseEntity 
{ 
    public const int NameLength = 40; 
    public const int PasswordMinLength = 5; 
    public const int PasswordMaxLength = 20; 
    public const int FullNameLength = 100; 
    public const int EmailLength = 100; 
    public const int PhoneLength = 40; //...
}

but my validation works only for Required and RegularExpression and doesn't work for MinLength and MaxLength and I don't get any error message in this case.

What can be a reason?

like image 389
Heidel Avatar asked Aug 16 '13 15:08

Heidel


People also ask

How to add validation in ASP net Core MVC?

Add validation rules to the movie model DataAnnotations also contains formatting attributes like DataType that help with formatting and don't provide any validation. Update the Movie class to take advantage of the built-in Required , StringLength , RegularExpression , and Range validation attributes.

Which of the following attributes validate string length?

The StringLength Attribute Specifies both the Min and Max length of characters that we can use in a field.

What is DataAnnotations MaxLength attribute?

Data Annotations - MaxLength Attribute in EF 6 & EF Core The MaxLength attribute specifies the maximum length of data value allowed for a property which in turn sets the size of a corresponding column in the database. It can be applied to the string or byte[] properties of an entity.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


2 Answers

MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database.

From MSDN:

Specifies the maximum length of array or string data allowed in a property.

StringLength is a data annotation that will be used for validation of user input.

From MSDN:

Specifies the minimum and maximum length of characters that are allowed in a data field.

Non Customized

Use [String Length]

[RegularExpression(@"^.{3,}$", ErrorMessage = "Minimum 3 characters required")]
[Required(ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "Maximum 30 characters")]

30 is the Max Length
Minimum length = 3

Customized StringLengthAttribute Class

public class MyStringLengthAttribute : StringLengthAttribute
{
    public MyStringLengthAttribute(int maximumLength)
        : base(maximumLength)
    {
    }

    public override bool IsValid(object value)
    {
        string val = Convert.ToString(value);
        if (val.Length < base.MinimumLength)
            base.ErrorMessage = "Minimum length should be 3";
        if (val.Length > base.MaximumLength)
            base.ErrorMessage = "Maximum length should be 6";
        return base.IsValid(value);
    }
}

public class MyViewModel
{
    [MyStringLength(6, MinimumLength = 3)]
    public String MyProperty { get; set; }
}
like image 176
Imad Alazani Avatar answered Oct 21 '22 00:10

Imad Alazani


Try using this attribute, for example for password min length:

[StringLength(100, ErrorMessage = "Максимальная длина пароля 20 символов", MinimumLength = User.PasswordMinLength)]
like image 9
Daniele Avatar answered Oct 20 '22 23:10

Daniele