如何使类的属性仅在某些时候需要。使用 ADDRESS 类,我的模型将需要公司办公室的属性。对于场外设施(可能不存在),我需要将属性设置为可选。
我想将 CorpOffice 街道、城市、州和邮政编码设为必填。当使用 Validator.TryValidateObject(...) 验证整个应用程序时,我试图让验证结果集合具有缺少的属性。
[Owned]
public class Address : BaseModel
{
public string? Street { get => _Street; set { _Street = value; NotifyPropertyChanged(nameof(Street)); } }
private string? _Street;
public string? Mailing { get => _Mailing; set { _Mailing = value; NotifyPropertyChanged(nameof(Mailing)); } }
private string? _Mailing;
public string? City { get => _City; set { _City = value; NotifyPropertyChanged(nameof(City)); } }
private string? _City;
public virtual State? State { get => _State; set { _State = value; NotifyPropertyChanged(nameof(State)); } }
private State? _State;
public int? Zip { get => _Zip; set { _Zip = value; NotifyPropertyChanged(nameof(Zip)); } }
private int? _Zip;
public int? Zip4 { get => _Zip4; set { _Zip4 = value; NotifyPropertyChanged(nameof(Zip4)); } }
private int? _Zip4;
}
public class Application
{
[Required]
public Address CorpOffice { get; set; }
public Address Offsite_Facility { get; set; }
}
所以根据pjs对我的问题的评论,这就是我想出的。
public class Application : IValidatableObject
{
// Other properties that require validation decorated with the [Required] attribute
public Address CorpOffice { get; set; }
public Address Offsite_Facility { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
if (string.IsNullOrEmpty(CorpOffice.Street))
results.Add(new ValidationResult("Street address is required", ["CorpOffice.Street"]));
if (string.IsNullOrEmpty(CorpOffice.City))
results.Add(new ValidationResult("City is required", ["CorpOffice.City"]));
if (CorpOffice.State == null || CorpOffice.State.StateId == 0)
results.Add(new ValidationResult("State is required", ["CorpOffice.State"]));
if (CorpOffice.Zip == null || CorpOffice.Zip == 0)
results.Add(new ValidationResult("Zip Code is required", ["CorpOffice.Zip"]));
if (CorpOffice.Zip > 99999)
results.Add(new ValidationResult("Zip Code cannot be more than 5 numbers", ["CorpOffice.Zip"]));
if(CorpOffice.Zip4 > 9999)
results.Add(new ValidationResult("Zip + 4 cannot be more than 4 numbers", ["CorpOffice.Zip4"]));
if (string.IsNullOrEmpty(Offsite_Facility.Street))
results.Add(new ValidationResult("Street address is required", ["Offsite_Facility.Street"]));
if (string.IsNullOrEmpty(Offsite_Facility.City))
results.Add(new ValidationResult("City is required", ["Offsite_Facility.City"]));
if (Offsite_Facility.State == null || Offsite_Facility.State.StateId == 0)
results.Add(new ValidationResult("State is required", ["Offsite_Facility.State"]));
if (Offsite_Facility.Zip == null || Offsite_Facility.Zip == 0)
results.Add(new ValidationResult("Zip Code is required", ["Offsite_Facility.Zip"]));
if (Offsite_Facility.Zip > 99999)
results.Add(new ValidationResult("Zip Code cannot be more than 5 numbers", ["Offsite_Facility.Zip"]));
if (Offsite_Facility.Zip4 > 9999)
results.Add(new ValidationResult("Zip + 4 cannot be more than 4 numbers", ["Offsite_Facility.Zip4"]));
return results;
}
}
然后我按照我的逻辑运行检查
...
ValidationResults = [];
// If there are validation errors, fill the results collection
Validator.TryValidateObject(NewApplication, new ValidationContext(NewApplication), ValidationResults, validateAllProperties: true);
// Run the custom validation
foreach(var val in NewApplication.Validate(new ValidationContext(NewApplication)))
ValidationResults.Add(val);
...