Validate String Array In ASP.NET MVC in client side

拟墨画扇 提交于 2021-02-20 05:13:01

问题


This is my first post.

I need string array validation such like below.

[Required(ErrorMessage = "Content name is required")]
public string[] ContentName { get; set; }

I found a post which has the same situation.

This answer and following code helped me so much and I could solve my problem.

public class StringArrayRequiredAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid (object value, ValidationContext validationContext)
    {
        string[] array = value as string[];

        if(array == null || array.Any(item => string.IsNullOrEmpty(item)))
        {
            return new ValidationResult(this.ErrorMessage);
        }
        else
        {
            return ValidationResult.Success;
        }
    }
}

And

[StringArrayRequired(ErrorMessage = "Content name is required")]
public string[] ContentName { get; set; }

But now I found an another problem. This validation works only server side. I wish I could have a client validation too. Because it would make my client much happier!!

So would you give me a nice way for this? Waiting for your answers!!


Thank you for your help. I write a short code in my view.

$.validator.addMethod('stringarrayrequired', function (value, element, params) {

    let array = value;

    if (array == null) {
        return false;
    }

    for (var i = 0; i < array.length; i++) {
        if (!array[i]) {
            return false;
        }
    }
    return true;
}, '');



$.validator.unobtrusive.adapters.add("stringarrayrequired", function (options) {
    options.rules["stringarrayrequired"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
    options.messages["stringarrayrequired"] = options.message;
});

(Sorry, I'm not fluent in JS...) And I add id="stringarrayrequired" to my . But it doesn't work. I also checked html code. When I click the submit button, there should be a class="input-validation-error" or "valid" in input tag for "ContentName", but I couldn't find both of them.

I still need more info... Anyone help?


I found a way to solve my problem.

(I changed property name ContextName to Selection)

[Display(Name = "Selections")]
public Selection[] Selections { get; set; } 

public class Selection
{
    [Required(ErrorMessage = "SelectionItem is empty")]
    public string SelectionItem { get; set; }
}

I use Selections for , SelectionItem for and .

As you know, [Required] attribute doesn't work for string[]. So I created a Selection class and changed string[] to Selection[], and applied [Required] attribute to string.

I know that this's not a clean way... I'll use foolproof or something.


回答1:


Add the following javaScript code in your view:

$.validator.addMethod('stringarrayrequired', function (value, element, params) {
    // here return true or false based on checking the input value
},'');


$.validator.unobtrusive.adapters.add("stringarrayrequired", function (options) {
    options.rules["stringarrayrequired"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
    options.messages["stringarrayrequired"] = options.message;
});


来源:https://stackoverflow.com/questions/54193129/validate-string-array-in-asp-net-mvc-in-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!