public void AddFieldError(string fieldName, string fieldMessage)
    {
        if (string.IsNullOrWhiteSpace(fieldName))
        {
            throw new ArgumentException("Empty field name");
        }
        if (string.IsNullOrWhiteSpace(fieldMessage))
        {
            throw new ArgumentException("Empty field message");
        }
        // appending error to existing one, if field already contains a message
        var existingFieldError = FieldErrors.FirstOrDefault(e => e.FieldName.Equals(fieldName));

        if (existingFieldError == null)
        {
            FieldErrors.Add(new FieldError {
                FieldName = fieldName, FieldMessage = fieldMessage
            });
        }
        else
        {
            existingFieldError.FieldMessage = $"{existingFieldError.FieldMessage}. {fieldMessage}";
        }
        IsError = true;
    }
        public ActionResult FailureSubmit(FormCollection values)
        {
            var errors = new FieldErrors();

            foreach (var key in values.Keys)
            {
                errors.Add(new FieldError(key.ToString(), "Test error for " + key.ToString()));
            }

            return this.FormPanel("Error is emulated", errors);
        }
        public ActionResult FailureSubmit(FormCollection values)
        {
            var errors = new FieldErrors();

            foreach (var key in values.Keys)
            {
                errors.Add(new FieldError(key.ToString(), "Test error for " + key.ToString()));
            }

            return(this.FormPanel("Error is emulated", errors));
        }
Beispiel #4
0
        public ValidationError AddError(string field, string error)
        {
            string fieldCamel = char.ToLowerInvariant(field[0]) + field.Substring(1);

            if (FieldErrors.TryGetValue(fieldCamel, out IList <string> list))
            {
                list.Add(error);
            }
            else
            {
                FieldErrors.Add(fieldCamel, new List <string> {
                    error
                });
            }
            return(this);
        }
Beispiel #5
0
 public BindingErrorModel(ModelStateDictionary modelState)
 {
     foreach (KeyValuePair <string, ModelStateEntry> v in modelState)
     {
         if (v.Key != "")
         {
             string key = v.Key[0].ToString().ToLower() + v.Key.Substring(1);
             FieldErrors.Add(key, new List <string>());
             foreach (ModelError error in v.Value.Errors)
             {
                 FieldErrors[key].Add(error.ErrorMessage);
             }
         }
         else
         {
             foreach (ModelError error in v.Value.Errors)
             {
                 GlobalErrors.Add(error.ErrorMessage);
             }
         }
     }
 }
 /// <summary>
 /// Log an error against a specific field
 /// </summary>
 /// <param name="fieldName"></param>
 /// <param name="errorMessage"></param>
 public void AddFieldError(string fieldName, string errorMessage)
 {
     FieldErrors.Add(new FieldError(fieldName, errorMessage));
     Success = false;
 }