In my program, for an entity, I have a Save and a Submit button. For my entity, I have a field that determines whether of not another field is required. An example of such a scenario:
public class Question{
bool IsRequired {get; set;}
string Answer {get; set;}
}
I know that I can make a custom validation attribute to make Answer required based on the IsRequired field, but the problem is, I only want this validation when the user "submits" the form, and not just saves it.
What's the best approach to validate the entity server side?
I'm thinking about either making a IsValid method in my service class and returning a list of errors, and then adding then to the ModelState in the controller. Or, maybe using the custom validation attribute and do it client side and somehow disable validation when the save is clicked, and enable it for the submit button.
It seems like there should be a more elegant solution to this. Does anyone have any suggestions?
Edit: Here's the code I'm using for my attribute:
public class RequiredIfAttribute : ValidationAttribute
{
RequiredAttribute _innerAttribute = new RequiredAttribute();
private string _dependentProperty { get; set; }
private object _targetValue { get; set; }
public RequiredIfAttribute(string dependentProperty, object targetValue)
{
this._dependentProperty = dependentProperty;
this._targetValue = targetValue;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var field = validationContext.ObjectType.GetProperty(_dependentProperty);
if (field != null)
{
var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
if ((dependentValue == null && _targetValue == null) || (dependentValue.Equals(_targetValue)))
{
if (!_innerAttribute.IsValid(value))
{
string name = validationContext.DisplayName;
return new ValidationResult(ErrorMessage = name + " Is required.");
}
}
return ValidationResult.Success;
}
else
{
return new ValidationResult(FormatErrorMessage(_dependentProperty));
}
}
}
The controller method:
public ActionResult FormDetail(UserFormDTO model, string buttonType)
{
if (buttonType == "Save")
{
if (!ModelState.IsValid)
{
return FormDetail(model.UserFormID);
}
//Save code is here
}
else if (buttonType == "Submit")
{
if (!ModelState.IsValid)
{
return FormDetail(model.UserFormID);
}
//Submit code is here
}
return RedirectToAction("Forms");
}
UserFormDTO has a list of Questions.