Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Project set StringLength validation value from another property

I have an mvc razor page which contains a form with fields created dynamically from the database ie the field name, type (textbox etc) are set in the db table as well as StringLength

Is there some way in my model to set the StringLength on the field to be a value from one of the other properties?

The questions are built up using an editor template for the QuestionModel, these are the relevant fields in the QuestionModel;

public string QuestionTypeName { get; set; }  --> ie this is like TextBox, TextArea etc and is used to create the write EditorTemplate

[RequiredIf("Required", true)]
[StringLength(<<this is where I want to use the property FieldLength>>)]
public string Answer { get; set; }

[DisplayName("Question name")]
public string Question { get; set; }

public bool Required { get; set; }
public Int32 FieldLength { get; set; }
like image 215
MartinaL Avatar asked Mar 25 '26 00:03

MartinaL


1 Answers

You can write your own Custom Validation Attribute as follows

Implementation

    public class CustomValidation : ValidationAttribute
    {
          public CustomValidation(string otherproperty)
          : base("Your Error Message goes here")
          {
              OtherProperty = otherproperty;
          }

          public string OtherProperty { get; set; }

          public override string FormatErrorMessage(string name)
          {
               return string.Format(ErrorMessageString, name, OtherProperty);
          }

          protected override ValidationResult IsValid(object firstobject, ValidationContext validationContext)
          {
                var firstValue = firstobject;
                var secondValue = GetSecondObject(validationContext);

          if(firstValue !=null && secondValue!=null)
          {
                if (//Your Condition for validation failure)
                {
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
                }               
          }
                return ValidationResult.Success;
          }

         protected object GetSecondObject(
ValidationContext validationContext)
         {
                var propertyInfo = validationContext
                              .ObjectType
                              .GetProperty(OtherProperty);
               if (propertyInfo != null)
               {
                    var secondValue = propertyInfo.GetValue(
                    validationContext.ObjectInstance, null);
                    return secondValue as object;
               }
              return null;
        }
  }

Usage:

public class ClassName
{
    [CustomValidation("FieldLength")] // pass the property name 
    public string Answer { get; set; }
    .....
like image 183
Jatin patil Avatar answered Mar 26 '26 14:03

Jatin patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!