/// <summary> /// Determines whether a form is valid or not. /// </summary> /// <param name="form">The form.</param> /// <param name="collection">The collection.</param> /// <param name="manager">The manager.</param> /// <returns>true if form is valid, false otherwise.</returns> protected virtual bool IsValidForm(FormDescription form, FormCollection collection, HttpFileCollectionBase files, FormsManager manager) { this.SanitizeFormCollection(collection); var behaviorResolver = ObjectFactory.Resolve<IControlBehaviorResolver>(); foreach (var control in form.Controls) { if (control.IsLayoutControl) continue; Type controlType; if (control.ObjectType.StartsWith("~/")) controlType = FormsManager.GetControlType(control); else controlType = TypeResolutionService.ResolveType(behaviorResolver.GetBehaviorObjectType(control), true); if (!controlType.ImplementsGenericInterface(typeof(IFormElementController<>))) continue; var controlInstance = manager.LoadControl(control); var controlBehaviorObject = behaviorResolver.GetBehaviorObject(controlInstance); if (controlBehaviorObject is IFormFieldController<IFormFieldModel>) { var formField = (IFormFieldController<IFormFieldModel>)controlBehaviorObject; var multipleFiles = files.GetMultiple(formField.MetaField.FieldName); object fieldValue; if (multipleFiles != null && multipleFiles.Count() > 0) { fieldValue = (object)multipleFiles; } else { collection[formField.MetaField.FieldName] = collection[formField.MetaField.FieldName] ?? string.Empty; fieldValue = (object)collection[formField.MetaField.FieldName]; } if (!formField.Model.IsValid(fieldValue)) return false; } else { var formElement = (IFormElementController<IFormElementModel>)controlBehaviorObject; if (!formElement.IsValid()) return false; } } return true; }
/// <inheritDoc/> public virtual SubmitStatus TrySubmitForm(FormCollection collection, HttpFileCollectionBase files, string userHostAddress) { var manager = FormsManager.GetManager(); var form = manager.GetForm(this.FormId); var formEntry = new FormEntryDTO(form); var formSubmition = new FormsSubmitionHelper(); if (!this.ValidateFormSubmissionRestrictions(formSubmition, formEntry)) return SubmitStatus.RestrictionViolation; if (!this.IsValidForm(form, collection, files, manager)) return SubmitStatus.InvalidEntry; var formFields = new HashSet<string>(form.Controls.Select(this.FormFieldName).Where((f) => !string.IsNullOrEmpty(f))); var postedFiles = new Dictionary<string, List<FormHttpPostedFile>>(); for (int i = 0; i < files.AllKeys.Length; i++) { if (formFields.Contains(files.AllKeys[i])) { postedFiles[files.AllKeys[i]] = files.GetMultiple(files.AllKeys[i]).Where(f => !f.FileName.IsNullOrEmpty()).Select(f => new FormHttpPostedFile() { FileName = f.FileName, ContentLength = f.ContentLength, ContentType = f.ContentType, InputStream = f.InputStream }).ToList(); } } var formData = new List<KeyValuePair<string, object>>(collection.Count); for (int i = 0; i < collection.Count; i++) { if (formFields.Contains(collection.Keys[i])) { formData.Add(new KeyValuePair<string, object>(collection.Keys[i], collection[collection.Keys[i]])); } } formEntry.PostedData = formData; formEntry.Files = postedFiles; formSubmition.Save(formEntry); return SubmitStatus.Success; }