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 ?

2
  • 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. stackoverflow.com/questions/19726404/… Commented Sep 22, 2015 at 15:01
  • @ps2goat Thank you for the suggestion, it was something like that i was looking for :) Commented Sep 23, 2015 at 9:15

0

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.