2

I have a data attribute for the string length defined as below for 40 characters.

[Display(Name = "Name"), StringLength(40, ErrorMessage = "The name cannot be more than 40 characters")]
public string EmployeeName { get; set; }

Now the requirement is changed to get that value from the service.

Is there a way to get that value into these data attributes something like: string s = 50; //Let's say calls the service to get this value

[Display(Name = "Name"), StringLength(s, ErrorMessage = "The name cannot be more than {0} characters")]
public string EmployeeName { get; set; }
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Dev
  • 1,451
  • 20
  • 30

2 Answers2

1

You can not pass a non-constant value to an attribute, so your solution can not be implemented.

However, you can pass a const value from config file. If you are accepting behaviour like the following: validation of the string will be of single maximum length for a whole lifetime of application and to change it you should reboot an app, take a look at variables in application config.

If you does not accept this behaviour, the one of possible solutions is to store your MaxLength somewhere in database and create your own StringLengthAttribute, which will query DB (or another data source) during valigation in the following way:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
sealed class MyValidationAttribute : ValidationAttribute
{
    public MyValidationAttribute()
    {

    }

    public override bool IsValid(object value)
    {
        if (value != null && value.GetType() == typeof(string))
        {
            int maxLength = //query your data source

            return ((string)value).Length <= maxLength;
        }

        return base.IsValid(value);
    }
}

Another possible solution is to perform client-side validation instead of server-side. If you will query your data source from the client-side it will look better, than querying it from attribute, in my opinion.

Community
  • 1
  • 1
Mikhail Tulubaev
  • 4,141
  • 19
  • 31
0

You cannot pass dynamic value to the attribute, but you can retrieve the dynamic value it self inside the attribute implementation and pass only a key to the attribute. ex:

public class DynamicLengthAttribute : ValidationAttribute
{
    private string _lengthKey;

    public DynamicLengthAttribute (string lengthKey)
    {
     _lengthKey = lengthKey;
    }

    public override bool IsValid(object value)
    {
        if (value != null && value.GetType() == typeof(string))
        {
           //retrive teh max length from the database according to the lengthKey variable, if you will store it in web.config you can do:

           int maxLength = ConfigurationManager.AppSettings[_lengthKey];
            return ((string)value).Length <= maxLength;
        }

        return base.IsValid(value);
    }
}

and in your model

[DynamicLength(maxLengths="EmpNameMaxLength", ErrorMessage = "The name cannot be more than {0} characters")]
public string EmployeeName { get; set; }
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19