3

I pass in a string through a constructor that i want to use as my error message in my model class like this:

public class ContactForm
{
    public string NameError { get; set; }

    public ContactForm(string nameError)
    {
        NameError = nameError;
    }

    [Required(ErrorMessage = NameError)]
    public string Name { get; set; }
    //More properties
}

Here i get the exception:

An object reference is required for the non-static field, method, or property 'UmbracoSport.Models.ContactForm.NameError.get'   D:\Umbraco\Websites\UmbracoSport\UmbracoSport\Models\Custom\ContactForm.cs  19  34  UmbracoSport

If i make NameError static i get this error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type  D:\Umbraco\Websites\UmbracoSport\UmbracoSport\Models\Custom\ContactForm.cs  19  34  UmbracoSport

Is this in any way possible ?

Aeterna
  • 347
  • 6
  • 11
  • 2
    You can create a custom data annotation to inspect the object's properties to create an error message, but this only works on the server. To also get it to work on the client, you'll need to create and wire up a validation rule in javascript. http://stackoverflow.com/questions/19726404/client-side-validation-in-custom-validation-attribute-asp-net-mvc-4 – ps2goat Sep 22 '15 at 15:01
  • @ps2goat Thank you for the suggestion, it was something like that i was looking for :) – Aeterna Sep 23 '15 at 09:15

0 Answers0