public static FormFactoryModelStateErrors ToFfModelStateErrors(this ModelErrorCollection mvcErrors) { return(new FormFactoryModelStateErrors(mvcErrors.Select(e => new FormFactoryModelStateError() { ErrorMessage = e.ErrorMessage }))); }
/// <summary> /// Generates the error message details. /// </summary> /// <param name="modelState">State of the model.</param> /// <returns></returns> private string GenerateErrorMessageDetails(ModelStateDictionary modelState) { var errorMessages = new List <string>(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { errorMessages.AddRange( errors .Select(error => { if (!string.IsNullOrEmpty(error.ErrorMessage)) { return(error.ErrorMessage); } if (error.Exception != null && !string.IsNullOrEmpty(error.Exception.Message)) { return(error.Exception.Message); } return(string.Empty); }) .ToArray() ); } } return(string.Join(" <> ", errorMessages)); }
public ApiProblemDetails( ModelStateDictionary modelState, HttpStatusCode statusCode, string?message = null, IReadOnlyDictionary <string, string[]>?additionalInfo = null ) { Message = message; StatusCode = (int)statusCode; AdditionalInfo = additionalInfo; Dictionary <string, string[]> errors = new Dictionary <string, string[]>(StringComparer.Ordinal); foreach (KeyValuePair <string, ModelStateEntry> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; ModelErrorCollection errorCollection = keyModelStatePair.Value.Errors; if (errorCollection != null && errorCollection.Count != 0) { List <string> errorStrings = errorCollection.Select(GetErrorMessage).ToList(); if (keyModelStatePair.Value.AttemptedValue != null) { errorStrings.Add(keyModelStatePair.Value.AttemptedValue); } errors.Add(key, errorStrings.ToArray()); } } Errors = errors;
public HttpError(ModelStateDictionary modelStateDictionary) { foreach (KeyValuePair <string, ModelStateEntry> keyValuePair in modelStateDictionary) { var errorKey = $" invalid {keyValuePair.Key}"; ModelErrorCollection errors = keyValuePair.Value.Errors; if (errors != null && errors.Count > 0) { var errorDescription = string.Join(";", errors.Select(e => e.ErrorMessage)); this.Add(new HttpErrorModel(errorKey, errorDescription, string.Empty)); } } }
public static IDictionary <string, object> ToErrorDictionary(this ModelStateDictionary modelState) { var modelStateError = new Dictionary <string, object>(); foreach (KeyValuePair <string, ModelStateEntry> keyModelStatePair in modelState) { var key = keyModelStatePair.Key; ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { modelStateError.Add(key, errors.Select(error => error.ErrorMessage)); } } return(modelStateError); }
private List <string> ValidationResponse(ViewDataDictionary viewData) { int count = viewData.ModelState.Keys.Count(); List <string> errorMsgList = new List <string>(count); foreach (string keyProp in viewData.ModelState.Keys) { ModelErrorCollection collection = ViewData.ModelState[keyProp].Errors; errorMsgList.AddRange(collection.Select(m => m.ErrorMessage));//tesar stex linq em ogtagordzel? :D haa tesa, charji miha gtnel es message meka senc el chenq talis :D sovorelu hamara)))du harc tur //uxexs kaxel a arden :D } return(errorMsgList); } //ay es gracs mi nayi, et hastat petq chi qez,, ok? lav :) de lav miqich el du qez u qez sovori vor harcer lini asa
/// <summary> /// Initializes a new instance of the <see cref="HttpError"/> class for <paramref name="modelState"/>. /// </summary> /// <param name="modelState">The invalid model state to use for error information.</param> /// <param name="includeErrorDetail"><c>true</c> to include exception messages in the error; <c>false</c> otherwise</param> public HttpError(ModelStateDictionary modelState, bool includeErrorDetail) : this() { if (modelState == null) { throw Error.ArgumentNull("modelState"); } if (modelState.IsValid) { throw Error.Argument("modelState", SRResources.ValidModelState); } Message = SRResources.BadRequest; HttpError modelStateError = new HttpError(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors .Select( error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? SRResources.ErrorOccurred : error.ErrorMessage); } } ) .ToArray(); modelStateError.Add(key, errorMessages); } } Add(HttpErrorKeys.ModelStateKey, modelStateError); }
/// <summary> /// Maps the validation errors from modelStateDictionary into the WebApi error representation /// </summary> /// <param name="modelStateDictionary">State of the model.</param> /// <returns>The list of validation errors, empty list if no errors</returns> private static IEnumerable <ValidationError> MapValidationErrors(ModelStateDictionary modelStateDictionary) { if (modelStateDictionary != null) { foreach (string key in modelStateDictionary.Keys) { ModelErrorCollection errors = modelStateDictionary[key].Errors; if (errors.Any()) { yield return(new ValidationError { Key = key, Errors = errors.Select(error => error.ErrorMessage).ToList() }); } } } }
public static Dictionary <string, IEnumerable <string> > GetErrorMessages(ModelStateDictionary modelState) { if (modelState == null) { throw new ArgumentNullException(nameof(modelState)); } Dictionary <string, IEnumerable <string> > modelErrors = new Dictionary <string, IEnumerable <string> >(); foreach (KeyValuePair <string, ModelStateEntry> keyModelStatePair in modelState) { ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => error.ErrorMessage); modelErrors.Add(keyModelStatePair.Key, errorMessages); } } return(modelErrors); }
public override void OnActionExecuting(ActionExecutingContext actionContext) { if (!actionContext.ModelState.IsValid) { var errors = new List <ErrorDetail>(); foreach (var modelState in actionContext.ModelState) { var errorKey = $" invalid {modelState.Key}"; ModelErrorCollection errorCollection = modelState.Value.Errors; if (errorCollection != null && errorCollection.Count > 0) { var errorDescription = string.Join(";", errorCollection.Select(e => e.ErrorMessage)); errors.Add(new ErrorDetail(errorKey, errorDescription, string.Empty)); } } _logger.LogInformation(string.Join("; ", errors)); actionContext.Result = new BadRequestObjectResult(errors); } }
/// <summary> /// Fetches a HttpError from a model state /// </summary> /// <param name="modelState"></param> /// <param name="includeErrorDetail"></param> /// <returns></returns> private HttpError GetErrors(ModelStateDictionary modelState, bool includeErrorDetail) { var modelStateError = new HttpError(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } return(String.IsNullOrEmpty(error.ErrorMessage) ? "ErrorOccurred" : error.ErrorMessage); }).ToArray(); modelStateError.Add(key, errorMessages); } } return(modelStateError); }
public ValidationError(string fieldName, ModelErrorCollection errorMessages) { this.FieldName = fieldName; this.ErrorMessages = errorMessages.Select(error => error.ErrorMessage).ToArray(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpErrorNegareh"/> class for <paramref name="modelState"/>. /// </summary> /// <param name="modelState">The invalid model state to use for error information.</param> /// <param name="includeErrorDetail"><c>true</c> to include exception messages in the error; <c>false</c> otherwise</param> public HttpErrorNegareh(ModelStateDictionary modelState, bool includeErrorDetail) : this() { if (modelState == null) { throw Error.ArgumentNull("modelState"); } if (modelState.IsValid) { throw Error.Argument("modelState", "The model state is valid." /*SRResources.ValidModelState*/); } Message = "The request is invalid.";// SRResources.BadRequest; HttpErrorNegareh modelStateError = new HttpErrorNegareh(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; var index = key.LastIndexOf('.'); key = key.Substring(index + 1, key.Length - index - 1); ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage); } }).ToArray(); modelStateError.Add(key, errorMessages); } } Add("NegarehErrorState", modelStateError); modelStateError = new HttpErrorNegareh(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage); } }).ToArray(); modelStateError.Add(key, errorMessages); } } Add(HttpErrorKeys.ModelStateKey, modelStateError); }
public static string ErrorsToString(this ModelErrorCollection collection) { return(string.Join(".", collection.Select(x => x.ErrorMessage))); }
private static Error GetErrorFromModelStateErrorCollection(ModelErrorCollection errors) { var errorMessages = errors.Select(e => e.ErrorMessage); return(new Error(string.Join(Environment.NewLine, errorMessages))); }
/// <summary> /// Convert MVC model errors into a single string /// </summary> /// <param name="errors">MVC model error collection</param> /// <returns>Comma separated string containing all errors.</returns> protected string ModelErrorString(ModelErrorCollection errors) { var messages = errors.Select(e => e.ErrorMessage); return String.Join(", ", messages); }
private static IHtmlString BuildSummaryErrorMessagesForKeyErrors <TModel>(this HtmlHelper <TModel> htmlHelper, ModelErrorCollection errors) => errors.Select(e => htmlHelper.BuildSummaryErrorMessage(e)).Concat();
public HttpErrorNegareh(ModelStateDictionary modelState, bool includeErrorDetail, params string[] modelNames) : this() { if (modelState == null) { throw Error.ArgumentNull("modelState"); } if (modelState.IsValid) { throw Error.Argument("modelState", "The model state is valid." /*SRResources.ValidModelState*/); } Message = "The request is invalid.";// SRResources.BadRequest; HttpErrorNegareh modelStateError = new HttpErrorNegareh(); List <HttpErrorNegareh> modelStateError2 = new List <HttpErrorNegareh>(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { if (!IsModel(keyModelStatePair.Key, modelNames)) { string key = keyModelStatePair.Key; var index = key.LastIndexOf('.'); key = key.Substring(index + 1, key.Length - index - 1); ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage); } }).ToArray(); modelStateError.Add(key, errorMessages); } } else { var akey = keyModelStatePair.Key.Split('.'); string key = keyModelStatePair.Key; var index = key.LastIndexOf('.'); key = key.Substring(index + 1, key.Length - index - 1); ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage); } }).ToArray(); HttpErrorNegareh tmodelStateError = new HttpErrorNegareh(); tmodelStateError.Add(key, errorMessages); modelStateError2.Add(tmodelStateError); } //key } } if (modelStateError2.Count() > 0) { HttpErrorNegareh modelStateErrorGrafLink = new HttpErrorNegareh(); for (int i = 0; i < modelStateError2.Count(); i++) { modelStateErrorGrafLink.Add($"{ i }", modelStateError2[i]);/*{ modelNames[0] }_*/ } //var ar = modelStateError2.ToArray(); modelStateError.Add("GraftLink", /*modelStateError2*/ modelStateErrorGrafLink); } Add("NegarehErrorState", modelStateError); modelStateError = new HttpErrorNegareh(); foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState) { string key = keyModelStatePair.Key; ModelErrorCollection errors = keyModelStatePair.Value.Errors; if (errors != null && errors.Count > 0) { IEnumerable <string> errorMessages = errors.Select(error => { if (includeErrorDetail && error.Exception != null) { return(error.Exception.Message); } else { return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage); } }).ToArray(); modelStateError.Add(key, errorMessages); } } Add(HttpErrorKeys.ModelStateKey, modelStateError); }