internal static FormContext ParseJsonOptions(JsonValidationOptions options) { // First hook up the form logic FormElement formElement = (FormElement)Document.GetElementById(options.FormId); DOMElement validationSummaryElement = (!ValidationUtil.StringIsNullOrEmpty(options.ValidationSummaryId)) ? Document.GetElementById(options.ValidationSummaryId) : null; FormContext formContext = new FormContext(formElement, validationSummaryElement); formContext.EnableDynamicValidation(); formContext.ReplaceValidationSummary = options.ReplaceValidationSummary; // Then hook up the field logic for (int i = 0; i < options.Fields.Length; i++) { JsonValidationField field = options.Fields[i]; DOMElement[] fieldElements = GetFormElementsWithName(formElement, field.FieldName); DOMElement validationMessageElement = (!ValidationUtil.StringIsNullOrEmpty(field.ValidationMessageId)) ? Document.GetElementById(field.ValidationMessageId) : null; FieldContext fieldContext = new FieldContext(formContext); ArrayList.AddRange((ArrayList)(object)fieldContext.Elements, fieldElements); fieldContext.ValidationMessageElement = validationMessageElement; fieldContext.ReplaceValidationMessageContents = field.ReplaceValidationMessageContents; // Hook up rules for (int j = 0; j < field.ValidationRules.Length; j++) { JsonValidationRule rule = field.ValidationRules[j]; Validator validator = ValidatorRegistry.GetValidator(rule); if (validator != null) { Validation validation = new Validation(); validation.FieldErrorMessage = rule.ErrorMessage; validation.Validator = validator; ArrayList.Add((ArrayList)(object)fieldContext.Validations, validation); } } fieldContext.EnableDynamicValidation(); ArrayList.Add((ArrayList)(object)formContext.Fields, fieldContext); } // hook up callback so that it can be executed by the AJAX code ArrayList registeredValidatorCallbacks = (ArrayList)Type.GetField(formElement, "validationCallbacks"); if (registeredValidatorCallbacks == null) { registeredValidatorCallbacks = new ArrayList(); Type.SetField(formElement, "validationCallbacks", registeredValidatorCallbacks); } Type.InvokeMethod(registeredValidatorCallbacks, "push", (ValidationCallback) delegate() { return(ValidationUtil.ArrayIsNullOrEmpty(formContext.Validate("submit"))); }); return(formContext); }
// not meant for public use, but must be marked public so that it's callable by the .jst public static void _Application_Load() { ArrayList allFormOptions = (ArrayList)Type.GetField(typeof(Window), "mvcClientValidationMetadata"); if (allFormOptions != null) { while (allFormOptions.Length > 0) { JsonValidationOptions thisFormOptions = (JsonValidationOptions)Type.InvokeMethod(allFormOptions, "pop"); ParseJsonOptions(thisFormOptions); } } }
internal static FormContext ParseJsonOptions(JsonValidationOptions options) { // First hook up the form logic FormElement formElement = (FormElement)Document.GetElementById(options.FormId); DOMElement validationSummaryElement = (!ValidationUtil.StringIsNullOrEmpty(options.ValidationSummaryId)) ? Document.GetElementById(options.ValidationSummaryId) : null; FormContext formContext = new FormContext(formElement, validationSummaryElement); formContext.EnableDynamicValidation(); formContext.ReplaceValidationSummary = options.ReplaceValidationSummary; // Then hook up the field logic for (int i = 0; i < options.Fields.Length; i++) { JsonValidationField field = options.Fields[i]; DOMElement[] fieldElements = GetFormElementsWithName(formElement, field.FieldName); DOMElement validationMessageElement = (!ValidationUtil.StringIsNullOrEmpty(field.ValidationMessageId)) ? Document.GetElementById(field.ValidationMessageId) : null; FieldContext fieldContext = new FieldContext(formContext); ArrayList.AddRange((ArrayList)(object)fieldContext.Elements, fieldElements); fieldContext.ValidationMessageElement = validationMessageElement; fieldContext.ReplaceValidationMessageContents = field.ReplaceValidationMessageContents; // Hook up rules for (int j = 0; j < field.ValidationRules.Length; j++) { JsonValidationRule rule = field.ValidationRules[j]; Validator validator = ValidatorRegistry.GetValidator(rule); if (validator != null) { Validation validation = new Validation(); validation.FieldErrorMessage = rule.ErrorMessage; validation.Validator = validator; ArrayList.Add((ArrayList)(object)fieldContext.Validations, validation); } } fieldContext.EnableDynamicValidation(); ArrayList.Add((ArrayList)(object)formContext.Fields, fieldContext); } // hook up callback so that it can be executed by the AJAX code ArrayList registeredValidatorCallbacks = (ArrayList)Type.GetField(formElement, "validationCallbacks"); if (registeredValidatorCallbacks == null) { registeredValidatorCallbacks = new ArrayList(); Type.SetField(formElement, "validationCallbacks", registeredValidatorCallbacks); } Type.InvokeMethod(registeredValidatorCallbacks, "push", (ValidationCallback)delegate() { return ValidationUtil.ArrayIsNullOrEmpty(formContext.Validate("submit")); }); return formContext; }