0

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.

4
  • You can look into foolproof, which builds the client side, custom validation for you. Commented Jan 15, 2019 at 6:49
  • I checked the link, it seems to be a great solution for my problem!! Does this work in ASP.NET Core 2.0?? Commented Jan 15, 2019 at 7:12
  • Yes, you can use it. Commented Jan 15, 2019 at 8:17
  • If you want to answer your own question, please don't include the answer in the question. Simply add your own answer at the bottom of the page. Commented Jan 16, 2019 at 5:11

1 Answer 1

0

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;
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your quick responce!! I'll try this code and check how it works.
It doesn't... Maybe there're some errors in the code I added. I described what I wrote in the question post above.
Can you share your screen via team viewer please?
Oh! I found a bit dirty way to solve my problem using only built-in reqiured attribute!! I'll share it with you, after I came back from the job :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.