ByFormField() public method

public ByFormField ( ) : List>.Dictionary
return List>.Dictionary
        public void ByFormField_UnderscoresNodes()
        {
            var errors = new ValidationErrors();
            errors.AddError("some-error", new ValidationError("some-error", "1", "some error"));
            errors.AddError("some-error", new ValidationError("some-error", "2", "some other error"));

            var nestedErrors = new ValidationErrors();
            nestedErrors.AddError("some-nested-error", new ValidationError("some-nested-error", "3", "some nested error"));
            nestedErrors.AddError("some-nested-error", new ValidationError("some-nested-error", "4", "some other nested error"));

            var nestedNestedErrors = new ValidationErrors();
            nestedNestedErrors.AddError("some-nested-nested-error", new ValidationError("some-nested-nested-error", "5", "some nested nested error"));
            nestedNestedErrors.AddError("some-nested-nested-error", new ValidationError("some-nested-nested-error", "6", "some other nested nested error"));

            nestedErrors.AddErrors("some-nested-object", nestedNestedErrors);
            errors.AddErrors("some-object", nestedErrors);

            Dictionary<string, List<string>> formErrors = errors.ByFormField();
            Assert.AreEqual("some error", formErrors["some_error"][0]);
            Assert.AreEqual("some other error", formErrors["some_error"][1]);

            Assert.AreEqual("some nested error", formErrors["some_object[some_nested_error]"][0]);
            Assert.AreEqual("some other nested error", formErrors["some_object[some_nested_error]"][1]);

            Assert.AreEqual("some nested nested error", formErrors["some_object[some_nested_object][some_nested_nested_error]"][0]);
            Assert.AreEqual("some other nested nested error", formErrors["some_object[some_nested_object][some_nested_nested_error]"][1]);
        }
Ejemplo n.º 2
0
        public void ByFormField_FlattensErrorsToFormElementNames()
        {
            var errors = new ValidationErrors();
            errors.AddError("some_error", new ValidationError("some_error", "1", "some error"));
            errors.AddError("some_error", new ValidationError("some_error", "2", "some other error"));

            var nestedErrors = new ValidationErrors();
            nestedErrors.AddError("some_nested_error", new ValidationError("some_nested_error", "3", "some nested error"));
            nestedErrors.AddError("some_nested_error", new ValidationError("some_nested_error", "4", "some other nested error"));

            var nestedNestedErrors = new ValidationErrors();
            nestedNestedErrors.AddError("some_nested_nested_error", new ValidationError("some_nested_nested_error", "5", "some nested nested error"));
            nestedNestedErrors.AddError("some_nested_nested_error", new ValidationError("some_nested_nested_error", "6", "some other nested nested error"));

            nestedErrors.AddErrors("some_nested_object", nestedNestedErrors);
            errors.AddErrors("some_object", nestedErrors);

            Dictionary<String, List<String>> formErrors = errors.ByFormField();
            Assert.AreEqual("some error", formErrors["some_error"][0]);
            Assert.AreEqual("some other error", formErrors["some_error"][1]);

            Assert.AreEqual("some nested error", formErrors["some_object[some_nested_error]"][0]);
            Assert.AreEqual("some other nested error", formErrors["some_object[some_nested_error]"][1]);

            Assert.AreEqual("some nested nested error", formErrors["some_object[some_nested_object][some_nested_nested_error]"][0]);
            Assert.AreEqual("some other nested nested error", formErrors["some_object[some_nested_object][some_nested_nested_error]"][1]);
        }