public void GetJsonValidationMetadata_ValidationSummary()
        {
            // Arrange
            FormContext context = new FormContext()
            {
                FormId = "theFormId", ValidationSummaryId = "validationSummary"
            };

            ModelClientValidationRule rule = new ModelClientValidationRule()
            {
                ValidationType = "ValidationType1", ErrorMessage = "Error Message"
            };

            rule.ValidationParameters["theParam"] = new { FirstName = "John", LastName = "Doe", Age = 32 };
            FieldValidationMetadata metadata = new FieldValidationMetadata()
            {
                FieldName = "theFieldName", ValidationMessageId = "theFieldName_ValidationMessage"
            };

            metadata.ValidationRules.Add(rule);
            context.FieldValidators["theFieldName"] = metadata;

            // Act
            string jsonMetadata = context.GetJsonValidationMetadata();

            // Assert
            string expected = @"{""Fields"":[{""FieldName"":""theFieldName"",""ReplaceValidationMessageContents"":false,""ValidationMessageId"":""theFieldName_ValidationMessage"",""ValidationRules"":[{""ErrorMessage"":""Error Message"",""ValidationParameters"":{""theParam"":{""FirstName"":""John"",""LastName"":""Doe"",""Age"":32}},""ValidationType"":""ValidationType1""}]}],""FormId"":""theFormId"",""ReplaceValidationSummary"":false,""ValidationSummaryId"":""validationSummary""}";

            Assert.Equal(expected, jsonMetadata);
        }
        public void GetJsonValidationMetadata_ValidationSummary()
        {
            // Arrange
            FormContext context = new FormContext() { FormId = "theFormId", ValidationSummaryId = "validationSummary" };

            ModelClientValidationRule rule = new ModelClientValidationRule() { ValidationType = "ValidationType1", ErrorMessage = "Error Message" };
            rule.ValidationParameters["theParam"] = new { FirstName = "John", LastName = "Doe", Age = 32 };
            FieldValidationMetadata metadata = new FieldValidationMetadata() { FieldName = "theFieldName", ValidationMessageId = "theFieldName_ValidationMessage" };
            metadata.ValidationRules.Add(rule);
            context.FieldValidators["theFieldName"] = metadata;

            // Act
            string jsonMetadata = context.GetJsonValidationMetadata();

            // Assert
            string expected = @"{""Fields"":[{""FieldName"":""theFieldName"",""ReplaceValidationMessageContents"":false,""ValidationMessageId"":""theFieldName_ValidationMessage"",""ValidationRules"":[{""ErrorMessage"":""Error Message"",""ValidationParameters"":{""theParam"":{""FirstName"":""John"",""LastName"":""Doe"",""Age"":32}},""ValidationType"":""ValidationType1""}]}],""FormId"":""theFormId"",""ReplaceValidationSummary"":false,""ValidationSummaryId"":""validationSummary""}";
            Assert.Equal(expected, jsonMetadata);
        }
Esempio n. 3
0
        private static void OutputClientValidation(HttpResponseBase response, FormContext formContext) {
            if (!formContext.ClientValidationEnabled) {
                return; // do nothing
            }

            // output a call that resembles:
            // _clientValidationFunction(validationObject, userContext);

            string validationJson = formContext.GetJsonValidationMetadata();

            string userContextJson = (formContext.ClientValidationState != null)
                ? new JavaScriptSerializer().Serialize(formContext.ClientValidationState)
                : "null";

            string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);
            string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines,
                formContext.ClientValidationFunction, validationJson, userContextJson);

            response.Write(formatted);
        }