private static bool VerifyValueUsability(ControllerContext controllerContext, ModelStateDictionary modelState, string modelStateKey, Type elementType, object value) { if (value == null && !TypeHelpers.TypeAllowsNullValue(elementType)) { if (modelState.IsValidField(modelStateKey)) { // a required entry field was left blank string message = GetValueRequiredResource(controllerContext); modelState.AddModelError(modelStateKey, message); } // we don't care about "you must enter a value" messages if there was an error return(false); } return(true); }
private static void AddValueRequiredMessageToModelState( ControllerContext controllerContext, ModelStateDictionary modelState, string modelStateKey, Type elementType, object value ) { if ( value == null && !TypeHelpers.TypeAllowsNullValue(elementType) && modelState.IsValidField(modelStateKey) ) { modelState.AddModelError( modelStateKey, GetValueRequiredResource(controllerContext) ); } }
private static IEnumerable <ModelState> GetFaultedModelStates(ModelStateDictionary modelState, string[] groupNames, object model) { var props = GetPropertyMap(string.Empty, model, new List <object>()); IEnumerable <string> validatedPropertyNames = props .Where(pdm => pdm.Property.Attributes.OfType <ValidationGroupAttribute>() .Where(vg => groupNames.Intersect(vg.GroupNames, StringComparer.OrdinalIgnoreCase).Any()).Any() ).Select(pd => pd.Key).ToArray(); IEnumerable <ModelState> faulted = modelState.Where(ms => validatedPropertyNames.Contains(ms.Key) && !modelState.IsValidField(ms.Key)).Select(ms => ms.Value).ToArray(); return(faulted); }