public static String JsonValidation(this System.Web.Http.ModelBinding.ModelStateDictionary state) { return(JsonConvert.SerializeObject( state.Where(x => x.Value.Errors.Count > 0) .Select(e => new { Name = e.Key, Errors = e.Value.Errors.Select(x => x.ErrorMessage) .Concat(e.Value.Errors.Where(x => x.Exception != null).Select(x => x.Exception.Message)) }) )); //return new JsonResult //{ // Data = new // { // Tag = "ValidationError", // State = from e in state // where e.Value.Errors.Count > 0 // select new // { // Name = e.Key, // Errors = e.Value.Errors.Select(x => x.ErrorMessage) // .Concat(e.Value.Errors.Where(x => x.Exception != null).Select(x => x.Exception.Message)) // } // } //}; }
public static ValidationMessages ToValidationMessages( this System.Web.Http.ModelBinding.ModelStateDictionary modelState, bool camelCaseKeyName = true) { var errors = modelState .Where(x => x.Value.Errors.Any()) .ToDictionary( kvp => CamelCasePropNames(kvp.Key), kvp => kvp.Value.Errors.Select(e => e.ErrorMessage) ); return(new ValidationMessages(errors)); }
public static void HandleModelState(this System.Web.Http.ModelBinding.ModelStateDictionary modelState) { if (!modelState.IsValid) { var errors = new Dictionary <string, string>(); modelState.Where(k => k.Value.Errors.Count > 0).ToList().ForEach(i => { var er = string.Join(", ", i.Value.Errors.Select(e => e.ErrorMessage).ToArray()); errors.Add(i.Key, er); }); throw new EntityValidationException(errors); } }
/// <summary> /// Chargement d'un dictionnaire simple pour renvoyer les informations de validation serveur /// "key => [ "error1", "error2", ... "errorN" ]". /// Suppression des entrées sans message /// </summary> /// <param name="modelState">ModelStateDictionary à renvoyer au client</param> /// <returns>Informations de validation</returns> public static Dictionary <string, string[]> ProcessModelState(System.Web.Http.ModelBinding.ModelStateDictionary modelState) { // TODO les clés peuevtn contenir uniquement le nom de la classe sans propriété à traiter ... pour le moment on passe var modelStateClean = modelState.Where(ms => ms.Key.Contains('.')).ToList(); return(modelStateClean .ToDictionary( entry => LowerFirstLetter(entry.Key.Split('.')[1]), // ex : emailVm.SelectedRole on ne conserve que selectedRole entry => entry.Value.Errors .Where(error => !string.IsNullOrEmpty(error.ErrorMessage) || error.Exception != null) .Select(error => !string.IsNullOrEmpty(error.ErrorMessage) ? error.ErrorMessage : error.Exception.Message).ToArray()) .Where(entry => entry.Value.Any()) .ToDictionary(entry => entry.Key, entry => entry.Value)); }
public static IEnumerable <FormResponseFieldErrors> GetErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState) { List <FormResponseFieldErrors> errors = new List <FormResponseFieldErrors>(); foreach (var fieldErrors in modelState.Where(x => x.Value.Errors.Any()).Select(x => new { x.Key, x.Value.Errors })) { errors.Add( new FormResponseFieldErrors() { Field = fieldErrors.Key, ErrorMessages = fieldErrors.Errors.Select(x => x.ErrorMessage) }); } return(errors); }