/// <summary> /// Performs field logic that cannot be represented with attributes for an observation. /// </summary> /// <param name="validationDictionary">A dictionary that will contain the resulting errors</param> /// <param name="observation">The observation to validate</param> /// <returns><c>true</c> for success, <c>false</c> for failure</returns> /// <remarks> /// This function should be used in conjunction with the built-in data annotations to validate /// simple conditions such as required. /// </remarks> public bool Validate(IValidationDictionary validationDictionary, Domain.Observation observation) { var type = _observationTypeRepository.GetById(observation.ObservationTypeId); //Email if (observation.EmailYesOrNo) { if (string.IsNullOrWhiteSpace(observation.Email)) { //Email validation is handled by the [EmailAddress] attribute on the domain validationDictionary.AddError("Email", Domain.Observation.Required); } } else { validationDictionary.Remove("Email"); observation.Email = null; } //Ensure type has a value first, may be a 0 / null type. if (type == null) { validationDictionary.AddError("ObservationTypeId", Domain.Observation.Required); } else { //Remove the built-in validation for observation category and other category //since we will override the rules to be more specific based on conditional //logic here. validationDictionary.Remove("ObservationCategoryId"); validationDictionary.Remove("OtherCategory"); if (type.Name == Domain.ObservationType.UnsafeCondition) { //A category is required if the observation type is "Unsafe Condition" if (!observation.ObservationCategoryId.HasValue) { validationDictionary.AddError("ObservationCategoryId", Domain.Observation.Required); } else { var category = _observationCategoryRepository.GetById(observation.ObservationCategoryId.Value); //If other specified, we need a reason (OtherCategory) if (category.Reference == Domain.ObservationCategory.Other && string.IsNullOrWhiteSpace(observation.OtherCategory)) { validationDictionary.AddError("OtherCategory", Domain.Observation.Required); } else { observation.OtherCategory = null; } } } else { //Remove category Id and other category if "Good Practice" is specified. observation.ObservationCategoryId = null; observation.OtherCategory = null; } } //Return whether the validation dictionary is valid. This is so that //we can extend the result given initially, for required etc. return(validationDictionary.IsValid); }