public ActionResult Demo5() { var attr1 = new Dictionary <string, string>(); attr1.Add("class", "form-control datepicker"); attr1.Add("placeholder", "Please Enter Birthday"); var birth1 = new TextBox { ResponseTitle = "Birthday1", Prompt = "Enter your birthday1:", DisplayOrder = 20, Required = true, RequiredMessage = "Your birthday1 is required", InputHtmlAttributes = attr1 }; var attr2 = new Dictionary <string, string>(); attr2.Add("class", "form-control"); attr2.Add("placeholder", "Please Enter Birthday"); var birth2 = new TextBox { Key = "Birthday", ResponseTitle = "Birthday2", Prompt = "Enter your birthday2:", DisplayOrder = 20, Required = true, RequiredMessage = "Your birthday2 is required", InputHtmlAttributes = attr2 }; var form = new Form(); form.AddFields(birth1); form.AddFields(birth2); return(this.View("Demo", form)); }
public ActionResult Demo4() { var attr = new Dictionary <string, string>(); attr.Add("class", "form-control"); attr.Add("placeholder", "Please Enter Name"); var name = new TextBox { ResponseTitle = "Name", Prompt = "Enter your full name:", DisplayOrder = 20, Required = true, RequiredMessage = "Your full name is required", InputHtmlAttributes = attr }; var form = new Form(); form.AddFields(name); return(this.View("Demo", form)); }
protected virtual void AddRadioButtonGroupBox(Form form, FieldAttributes fieldAttributes, double _Width, double _Height) { var radioGroupBoxValue = string.Empty; form.AddFields(GetGroupBox(fieldAttributes, _Width + 12, _Height, radioGroupBoxValue)); }
public virtual void UpdateHiddenFields(int pageNumber, Form form, System.Collections.Specialized.NameValueCollection postedForm) { double _Width, _Height; _Width = 1024; _Height = 768; var currentFieldDigests = GetFieldDigestsWithPageNumber(CurrentFormId, pageNumber); var otherPageFieldDigests = GetFieldDigestsNotWithPageNumber(CurrentFormId, pageNumber); foreach (var fieldDigest in otherPageFieldDigests) { bool IsFound = false; string Value = null; foreach (var key in postedForm.AllKeys.Where(x => x.StartsWith(form.FieldPrefix))) { string fieldKey = key.Remove(0, form.FieldPrefix.Length); if (fieldKey.Equals(fieldDigest.FieldName, StringComparison.OrdinalIgnoreCase)) { Value = postedForm[key]; IsFound = true; break; } } if (IsFound) { var fieldAttributes = GetFieldAttributes(fieldDigest); MvcDynamicForms.Fields.Field field = null; switch ((int)fieldDigest.FieldType) { case 1: // textbox var _TextBoxValue = Value; field = GetTextBox(fieldAttributes, _Width, _Height, _TextBoxValue); break; case 2: //Label/Title field = GetLabel(fieldAttributes, _Width, _Height); break; case 3: //Label break; case 4: //MultiLineTextBox var _TextAreaValue = Value; field = GetTextArea(fieldAttributes, _Width, _Height, _TextAreaValue); break; case 5: //NumericTextBox var _NumericTextBoxValue = Value; field = GetNumericTextBox(fieldAttributes, _Width, _Height, _NumericTextBoxValue); break; case 7: // 7 DatePicker var _DatePickerValue = Value; field = GetDatePicker(fieldAttributes, _Width, _Height, _DatePickerValue); break; case 8: //TimePicker var _timePickerValue = Value; field = GetTimePicker(fieldAttributes, _Width, _Height, _timePickerValue); break; case 10: //CheckBox var _CheckBoxValue = Value; field = GetCheckBox(fieldAttributes, _Width, _Height, _CheckBoxValue); break; case 11: //DropDown Yes/No var _DropDownSelectedValueYN = Value; if (_DropDownSelectedValueYN == "1") { _DropDownSelectedValueYN = "Yes"; } if (_DropDownSelectedValueYN == "0") { _DropDownSelectedValueYN = "No"; } var dropdownselectedvalueYN = GetDropDown(fieldAttributes, _Width, _Height, _DropDownSelectedValueYN, "Yes&#;No", 11); form.AddFields(dropdownselectedvalueYN); break; case 12: //RadioList var _RadioListSelectedValue1 = Value; string RadioListValues1 = ""; field = GetRadioList(fieldAttributes, _Width, _Height, _RadioListSelectedValue1); break; case 17: //DropDown LegalValues string DropDownValues1 = ""; var _DropDownSelectedValue1 = Value; field = GetDropDown(fieldAttributes, _Width, _Height, _DropDownSelectedValue1, DropDownValues1, 17); break; case 18: //DropDown Codes string DropDownValues2 = ""; var _DropDownSelectedValue2 = Value; field = GetDropDown(fieldAttributes, _Width, _Height, _DropDownSelectedValue2, DropDownValues2, 18, fieldAttributes.RelateCondition); break; case 19: //DropDown CommentLegal string DropDownValues = ""; var _DropDownSelectedValue = Value; field = GetDropDown(fieldAttributes, _Width, _Height, _DropDownSelectedValue, DropDownValues, 19); break; case 21: //GroupBox field = GetGroupBox(fieldAttributes, _Width, _Height, Value); break; } if (field != null) { field.IsPlaceHolder = true; form.AddFields(field); } } } }
protected virtual void AddFormFields(SurveyInfoDTO surveyInfo, int pageId, Dictionary <string, string> responseQA, Form form, double _Width, double _Height, string checkcode, StringBuilder javaScript) { var formId = surveyInfo.SurveyId; IEnumerable <FieldAttributes> currentPageFieldAttributes = surveyInfo.GetPageFieldAttributesByPageId(formId, pageId).Values; foreach (var fieldAttributes in currentPageFieldAttributes) { string fieldValue = null; fieldValue = (responseQA.TryGetValue(fieldAttributes.FieldName.ToLower(), out fieldValue) ? fieldValue : string.Empty); javaScript.Append(GetFormJavaScript(checkcode, form, fieldAttributes.FieldName)); fieldAttributes.IsHighlighted = GetControlState(fieldAttributes.FieldName, form.HighlightedFieldsList); switch (fieldAttributes.FieldType) { case FieldTypes.Text: // textbox form.AddFields(GetTextBox(fieldAttributes, _Width, _Height, fieldValue)); break; case FieldTypes.Label: //Label/Title form.AddFields(GetLabel(fieldAttributes, _Width, _Height)); break; case FieldTypes.UppercaseText: break; case FieldTypes.Multiline: //MultiLineTextBox form.AddFields(GetTextArea(fieldAttributes, _Width, _Height, fieldValue)); break; case FieldTypes.Number: //NumericTextBox form.AddFields(GetNumericTextBox(fieldAttributes, _Width, _Height, fieldValue)); break; case FieldTypes.Date: //DatePicker form.AddFields(GetDatePicker(fieldAttributes, _Width, _Height, fieldValue)); break; case FieldTypes.Time: //TimePicker form.AddFields(GetTimePicker(fieldAttributes, _Width, _Height, fieldValue)); break; case FieldTypes.DateTime: break; case FieldTypes.Checkbox: //CheckBox var checkbox = GetCheckBox(fieldAttributes, _Width, _Height, fieldValue); form.AddFields(checkbox); break; case FieldTypes.YesNo: //DropDown Yes/No if (fieldValue == "1" || fieldValue == "true") { fieldValue = "Yes"; } else if (fieldValue == "0" || fieldValue == "false") { fieldValue = "No"; } var dropdownSelectedValueYN = GetDropDown(fieldAttributes, _Width, _Height, fieldValue, "Yes&#;No", 11); form.AddFields(dropdownSelectedValueYN); break; case FieldTypes.Option: //RadioList AddRadioButtonGroupBox(form, fieldAttributes, _Width, _Height); var selectedRadioListValue = fieldValue; var radioListValues = fieldAttributes.List; form.AddFields(GetRadioList(fieldAttributes, _Width, _Height, selectedRadioListValue)); break; case FieldTypes.LegalValues: //DropDown LegalValues var dropDownLegalValues = string.Join("&#;", fieldAttributes.SourceTableValues); var selectedLegalValue = fieldValue; form.AddFields(GetDropDown(fieldAttributes, _Width, _Height, selectedLegalValue, dropDownLegalValues, (int)FieldTypes.LegalValues)); break; case FieldTypes.Codes: //DropDown Codes var dropDownCodesValues = string.Join("&#;", fieldAttributes.SourceTableValues); var selectedCodesValue = fieldValue; var dropDownSelectedCodesValue = GetDropDown(fieldAttributes, _Width, _Height, selectedCodesValue, dropDownCodesValues, (int)FieldTypes.Codes, fieldAttributes.RelateCondition); form.AddFields(dropDownSelectedCodesValue); break; case FieldTypes.CommentLegal: //DropDown CommentLegal var dropDownCommentLegalValues = string.Join("&#;", fieldAttributes.SourceTableValues); var selectedCommentLegalValue = fieldValue; var dropDownSelectedCommentLegalValue = GetDropDown(fieldAttributes, _Width, _Height, selectedCommentLegalValue, dropDownCommentLegalValues, 19); form.AddFields(dropDownSelectedCommentLegalValue); break; case FieldTypes.Relate: //RelateButton form.AddFields(GetRelateButton(fieldAttributes, _Width, _Height)); break; case FieldTypes.Group: //GroupBox form.AddFields(GetGroupBox(fieldAttributes, _Width, _Height, fieldValue)); break; } } }
public static Form GetForm(List <PropertyInfo> properties) { Form form = new Form(); foreach (var property in properties) { DisplayAttribute displayAttribute = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute)); RequiredAttribute requiredAttribute = (RequiredAttribute)property.GetCustomAttribute(typeof(RequiredAttribute)); EmailAddressAttribute emailAttribute = (EmailAddressAttribute)property.GetCustomAttribute(typeof(EmailAddressAttribute)); StringLengthAttribute lengthAttribute = (StringLengthAttribute)property.GetCustomAttribute(typeof(StringLengthAttribute)); RegularExpressionAttribute regexAttribute = (RegularExpressionAttribute)property.GetCustomAttribute(typeof(RegularExpressionAttribute)); ShowAsListAttribute showAsListAttribute = (ShowAsListAttribute)property.GetCustomAttribute(typeof(ShowAsListAttribute)); if (displayAttribute.AutoGenerateField) { string title = displayAttribute.Name; string description = displayAttribute.Description; string group = displayAttribute.GroupName; string type = null; string typeMessage = null; string lengthMessage = null; int lengthMin = -1; int lengthMax = -1; string regex = null; string regexMessage = null; bool required = true; #region Attributes #region General if (displayAttribute.ResourceType != null) { title = GetResource(title, displayAttribute.ResourceType); description = GetResource(description, displayAttribute.ResourceType); group = GetResource(group, displayAttribute.ResourceType); } #endregion #region Email Type if (emailAttribute != null) { type = "email"; typeMessage = emailAttribute.ErrorMessage; if (emailAttribute.ErrorMessageResourceType != null) { typeMessage = GetResource(emailAttribute.ErrorMessageResourceName, emailAttribute.ErrorMessageResourceType); typeMessage = typeMessage == null || typeMessage == emailAttribute.ErrorMessageResourceName ? "" : string.Format(typeMessage, title); } } #endregion #region RegEx if (regexAttribute != null) { regex = regexAttribute.Pattern; regexMessage = regexAttribute.ErrorMessage; if (regexAttribute.ErrorMessageResourceType != null) { regexMessage = GetResource(regexAttribute.ErrorMessageResourceName, regexAttribute.ErrorMessageResourceType); } } #endregion #region Required if (requiredAttribute != null) { required = true; } #endregion #region Length if (lengthAttribute != null) { lengthMin = lengthAttribute.MinimumLength; lengthMax = lengthAttribute.MaximumLength; if (lengthAttribute.ErrorMessageResourceType != null) { lengthMessage = GetResource(lengthAttribute.ErrorMessageResourceName, lengthAttribute.ErrorMessageResourceType); lengthMessage = lengthMessage == null || lengthMessage == lengthAttribute.ErrorMessageResourceName ? "" : string.Format(lengthMessage, title, lengthMax, lengthMin); } } #endregion #endregion #region Property generator if (property.PropertyType == typeof(System.String)) { #region String if (showAsListAttribute != null) { #region String as List string placeholder = showAsListAttribute.Placeholder; if (placeholder != null) { var placeholderTemp = placeholder; placeholder = GetResource(placeholderTemp, showAsListAttribute.ResourceType); placeholder = placeholder == placeholderTemp ? placeholderTemp : placeholder; } var s = new Select { Key = property.Name, MappedName = property.Name, Description = description, DisplayOrder = displayAttribute.Order, Title = title, Group = displayAttribute.GroupName, GroupTitle = group, Required = required, Placeholder = placeholder, RequiredMessage = string.Format("{0} is required", title) }; MethodInfo info = showAsListAttribute.DataSourceType.GetMethod(showAsListAttribute.DataSourceMethod); List <SelectListItem> selectList = null; if (info != null) { selectList = (List <SelectListItem>)info.Invoke(null, null); } if (selectList != null) { foreach (var item in selectList) { s.Choices.Add(new ListItem() { Text = item.Text, Value = item.Value, Selected = property.GetValue(null).ToString() == item.Value }); } } form.AddFields(s); #endregion } else { var val = property.GetValue(null); #region String var tb = new TextBox { Key = property.Name, MappedName = property.Name, Description = description, DisplayOrder = displayAttribute.Order, Title = title, Group = displayAttribute.GroupName, GroupTitle = group, Required = required, RequiredMessage = string.Format("{0} is required", title), Value = val == null ? null : val.ToString(), Type = type, TypeMessage = typeMessage, LengthMin = lengthMin, LengthMax = lengthMax, LengthMessage = lengthMessage, RegularExpression = regex, RegexMessage = regexMessage }; form.AddFields(tb); #endregion } #endregion } else if (property.PropertyType == typeof(System.Int16) || property.PropertyType == typeof(System.Int32) || property.PropertyType == typeof(System.Int64)) { #region Number type = "number"; var tb = new TextBox { Key = property.Name, MappedName = property.Name, Description = description, DisplayOrder = displayAttribute.Order, Title = title, Group = displayAttribute.GroupName, GroupTitle = group, Required = required, RequiredMessage = string.Format("{0} is required", title), Value = property.GetValue(null).ToString(), Type = type, TypeMessage = typeMessage, RegularExpression = regex, RegexMessage = regexMessage }; form.AddFields(tb); #endregion } else if (property.PropertyType == typeof(System.Boolean)) { #region Boolean var cb = new CheckBox { Key = property.Name, MappedName = property.Name, Description = description, DisplayOrder = displayAttribute.Order, Title = title, Group = displayAttribute.GroupName, GroupTitle = group, Required = required, RequiredMessage = string.Format("{0} is required", title), Checked = (bool)property.GetValue(null) }; form.AddFields(cb); #endregion } else if (property.PropertyType.BaseType == typeof(System.Enum)) { #region Enum var val = property.GetValue(null); var s = new Select { Key = property.Name, MappedName = property.Name, Description = description, DisplayOrder = displayAttribute.Order, Title = title, Group = displayAttribute.GroupName, GroupTitle = group, MultipleSelection = true, Required = required, RequiredMessage = string.Format("{0} is required", title), Size = 5 }; foreach (var item in System.Web.Mvc.Html.EnumHelper.GetSelectList(property.PropertyType)) { if (item.Value != "0") { s.Choices.Add(new ListItem() { Text = item.Text, Value = item.Value, Selected = ((int)val & int.Parse(item.Value)) == int.Parse(item.Value) }); } } form.AddFields(s); #endregion } else { Debug.WriteLine(string.Format("Unhandled property type for form generator: {0}", property.PropertyType)); } #endregion } } return(form); }
public static Form GetForm() { /* * This method sets up the Form and Field objects that * are needed to dynamically generate html forms at runtime. * * Of course, there are other ways of going about defining your forms and their fields. * I used a static class in this demo application for simplicity. * In the real world, you could store your field definitions anywhere. * * For example, you could create a database table to store all * of the data needed to create the form fields below. * Some of your end users could have access to some kind of interface to create, update, * or delete the form field definitions in the database. * This described scenario was actually the inspiration for this project. * * There are 7 different Field types that can be used to construct the form: * - TextBox (single line text input) * - Textarea (multi line text input) * - Checkbox * - CheckboxList * - RadioList * - Select (Drop down lists and List boxes) * - Literal (Any custom html at all. For display purposes only (no user input)) * * Each Field type have a few things in common: * - Title property: Used when storing end user's responses. * - Prompt property: Question asked to the user for each field. * - DisplayOrder property: The order that the field is displayed to the user. * - Required property: Is the user required to complete the field? * - InputHtmlAttributes: Allows the developer to set the input elements html attributes * * There are other properties and behaviors that some Field types do not share with each other. * Take a look through the members of each Type to see what you can do. * Much of each type's unique functionality is demonstrated below. * Feel free to tinker around in this file, changing and adding fields. * Don't forget to add newly created fields to the Form. * * The Form object is the object that contains all of your Field objects, * triggers validation and rendering, and lets the developer access user responses. * When constructing your form, you can use Form.AddFields() to get your Fields * into the form (imagine that!). * * Check out * /Controllers/HomeController.cs * /Views/Home/Demo.cshtml * /Views/Home/Responses.cshtml * to learn how to use the Form object in your web application. */ // create fields var description = new Literal { Key = "description", Template = String.Format("<p>{0}</p>", PlaceHolders.Literal), DisplayOrder = 10, Html = "This is a dynamically generated form. All of the input fields on this form are generated at runtime." }; var name = new TextBox { ResponseTitle = "Name", Prompt = "Enter your full name:", DisplayOrder = 20, Required = true, RequiredMessage = "Your full name is required", }; var gender = new RadioList { DisplayOrder = 30, ResponseTitle = "Gender", Prompt = "Select your gender:", Required = true, Orientation = Orientation.Vertical }; gender.AddChoices("Male,Female", ","); var email = new TextBox { DisplayOrder = 25, ResponseTitle = "Email Address", Prompt = "Enter your email address:", Required = true, RegexMessage = "Must be a valid email address", RegularExpression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" }; var sports = new CheckBoxList { DisplayOrder = 40, ResponseTitle = "Favorite Sports", Prompt = "What are your favorite sports?", Orientation = Orientation.Horizontal }; sports.AddChoices("Baseball,Football,Soccer,Basketball,Tennis,Boxing,Golf", ","); var states = new Select { DisplayOrder = 50, ResponseTitle = "Visited States", MultipleSelection = true, Size = 10, Prompt = "What US states have you visited? (Use the ctrl key to select multiple states.)" }; states.AddChoices( "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming", ","); var bio = new TextArea { DisplayOrder = 60, ResponseTitle = "Bio", Prompt = "Describe yourself:" }; bio.InputHtmlAttributes.Add("cols", "40"); bio.InputHtmlAttributes.Add("rows", "6"); var month = new Select { DisplayOrder = 70, ResponseTitle = "Month Born", Prompt = "What month were you born in?", ShowEmptyOption = true, EmptyOption = "- Select One - " }; month.AddChoices("January,February,March,April,May,June,July,August,September,October,November,December", ","); var agree = new CheckBox { DisplayOrder = 80, ResponseTitle = "Agrees To Terms", Prompt = "I agree to all of the terms in the EULA.", Required = true, RequiredMessage = "You must agree to the EULA!" }; var eula = new Literal { DisplayOrder = 75, Html = string.Format(@"<textarea readonly=""readonly"" rows=""8"" cols=""60"">{0}</textarea>", GetEULA()) }; var file = new FileUpload { Prompt = "Your photo", InvalidExtensionError = "Image files only.", ValidExtensions = ".jpg,.gif,.png", Required = true, DisplayOrder = 73, UseMultiple = true }; file.Validated += new ValidatedEventHandler(file_Validated); file.Posted += new FilePostedEventHandler(file_Posted); var hidden = new Hidden { ResponseTitle = "A Hidden Field", Value = "some value" }; // create form and add fields to it var form = new Form(); form.AddFields(description, name, gender, email, sports, states, bio, month, agree, eula, file, hidden); return(form); }
public static Form GetFormWithData() { // create fields var description = new Literal { Key = "description", Template = String.Format("<p>{0}</p>", PlaceHolders.Literal), DisplayOrder = 10, Html = "This is a dynamically generated form. All of the input fields on this form are generated at runtime." }; description.AddDataValue("Description", "This is a test description", true); var name = new TextBox { ResponseTitle = "Name", Prompt = "Enter your full name:", DisplayOrder = 20, Required = true, RequiredMessage = "Your full name is required" }; name.AddDataValue("Name", "Vishal Sharma",true); var gender = new RadioList { DisplayOrder = 30, ResponseTitle = "Gender", Prompt = "Select your gender:", Required = true, Orientation = Orientation.Vertical }; gender.AddChoices("Male,Female", ","); gender.AddDataValue("Gender", "Male", true); var email = new TextBox { DisplayOrder = 25, ResponseTitle = "Email Address", Prompt = "Enter your email address:", Required = true, RegexMessage = "Must be a valid email address", RegularExpression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" }; email.AddDataValue("EmailAddress", "*****@*****.**", true); var sports = new CheckBoxList { DisplayOrder = 40, ResponseTitle = "Favorite Sports", Prompt = "What are your favorite sports?", Orientation = Orientation.Horizontal }; sports.AddChoices("Baseball,Football,Soccer,Basketball,Tennis,Boxing,Golf", ","); sports.AddDataValue("Favorite Sports", "Cricket", true); var states = new Select { DisplayOrder = 50, ResponseTitle = "Visited States", MultipleSelection = true, Size = 10, Prompt = "What US states have you visited? (Use the ctrl key to select multiple states.)" }; states.AddChoices("Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming", ","); states.AddDataValue("Visited States", "New York", true); var bio = new TextArea { DisplayOrder = 60, ResponseTitle = "Bio", Prompt = "Describe yourself:" }; bio.InputHtmlAttributes.Add("cols", "40"); bio.InputHtmlAttributes.Add("rows", "6"); bio.AddDataValue("Bio", "I am a software engineer.", true); var month = new Select { DisplayOrder = 70, ResponseTitle = "Month Born", Prompt = "What month were you born in?", ShowEmptyOption = true, EmptyOption = "- Select One - " }; month.AddChoices("January,February,March,April,May,June,July,August,September,October,November,December", ","); month.AddDataValue("Month Born", "May", true); var agree = new CheckBox { DisplayOrder = 80, ResponseTitle = "Agrees To Terms", Prompt = "I agree to all of the terms in the EULA.", Required = true, RequiredMessage = "You must agree to the EULA!" }; agree.AddDataValue("Agree To Terms", "Yes", true); var eula = new Literal { DisplayOrder = 75, Html = string.Format(@"<textarea readonly=""readonly"" rows=""8"" cols=""60"">{0}</textarea>", GetEULA()) }; var file = new FileUpload { Prompt = "Your photo", InvalidExtensionError = "Image files only.", ValidExtensions = ".jpg,.gif,.png", DisplayOrder = 73, }; file.Validated += new ValidatedEventHandler(file_Validated); file.Posted += new FilePostedEventHandler(file_Posted); var hidden = new Hidden { ResponseTitle = "A Hidden Field", Value = "some value" }; // create form and add fields to it var form = new Form(); form.AddFields(description, name, gender, email, sports, states, bio, month, agree, eula, file, hidden); return form; }
public static Form GetForm() { /* * This method sets up the Form and Field objects that * are needed to dynamically generate html forms at runtime. * * Of course, there are other ways of going about defining your forms and their fields. * I used a static class in this demo application for simplicity. * In the real world, you could store your field definitions anywhere. * * For example, you could create a database table to store all * of the data needed to create the form fields below. * Some of your end users could have access to some kind of interface to create, update, * or delete the form field definitions in the database. * This described scenario was actually the inspiration for this project. * * There are 7 different Field types that can be used to construct the form: * - TextBox (single line text input) * - Textarea (multi line text input) * - Checkbox * - CheckboxList * - RadioList * - Select (Drop down lists and List boxes) * - Literal (Any custom html at all. For display purposes only (no user input)) * * Each Field type have a few things in common: * - Title property: Used when storing end user's responses. * - Prompt property: Question asked to the user for each field. * - DisplayOrder property: The order that the field is displayed to the user. * - Required property: Is the user required to complete the field? * - InputHtmlAttributes: Allows the developer to set the input elements html attributes * * There are other properties and behaviors that some Field types do not share with each other. * Take a look through the members of each Type to see what you can do. * Much of each type's unique functionality is demonstrated below. * Feel free to tinker around in this file, changing and adding fields. * Don't forget to add newly created fields to the Form. * * The Form object is the object that contains all of your Field objects, * triggers validation and rendering, and lets the developer access user responses. * When constructing your form, you can use Form.AddFields() to get your Fields * into the form (imagine that!). * * Check out * /Controllers/HomeController.cs * /Views/Home/Demo.aspx * /Views/Home/Responses.aspx * to learn how to use the Form object in your web application. */ // create fields var description = new Literal { Key = "description", Template = String.Format("<p>{0}</p>", PlaceHolders.Literal), DisplayOrder = 10, Html = "This is a dynamically generated form. All of the input fields on this form are generated at runtime." }; var name = new TextBox { ResponseTitle = "Name", Prompt = "Enter your full name:", DisplayOrder = 20, Required = true, RequiredMessage = "Your full name is required" }; var gender = new RadioList { DisplayOrder = 30, ResponseTitle = "Gender", Prompt = "Select your gender:", Required = true, Orientation = Orientation.Vertical }; gender.AddChoices("Male,Female", ","); var email = new TextBox { DisplayOrder = 25, ResponseTitle = "Email Address", Prompt = "Enter your email address:", Required = true, RegexMessage = "Must be a valid email address", RegularExpression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" }; var sports = new CheckBoxList { DisplayOrder = 40, ResponseTitle = "Favorite Sports", Prompt = "What are your favorite sports?", Orientation = Orientation.Horizontal }; sports.AddChoices("Baseball,Football,Soccer,Basketball,Tennis,Boxing,Golf", ","); var states = new Select { DisplayOrder = 50, ResponseTitle = "Visited States", MultipleSelection = true, Size = 10, Prompt = "What US states have you visited? (Use the ctrl key to select multiple states.)" }; states.AddChoices("Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming", ","); var bio = new TextArea { DisplayOrder = 60, ResponseTitle = "Bio", Prompt = "Describe yourself:" }; bio.InputHtmlAttributes.Add("cols", "40"); bio.InputHtmlAttributes.Add("rows", "6"); var month = new Select { DisplayOrder = 70, ResponseTitle = "Month Born", Prompt = "What month were you born in?", ShowEmptyOption = true, EmptyOption = "- Select One - " }; month.AddChoices("January,February,March,April,May,June,July,August,September,October,November,December", ","); var agree = new CheckBox { DisplayOrder = 80, ResponseTitle = "Agrees To Terms", Prompt = "I agree to all of the terms in the EULA.", Required = true, RequiredMessage = "You must agree to the EULA!" }; var eula = new Literal { DisplayOrder = 75, Html = string.Format(@"<textarea readonly=""readonly"" rows=""8"" cols=""60"">{0}</textarea>", GetEULA()) }; var file = new FileUpload { Prompt = "Your photo", InvalidExtensionError = "Image files only.", ValidExtensions = ".jpg,.gif,.png", Required = true, DisplayOrder = 73, }; file.Validated += new ValidatedEventHandler(file_Validated); file.Posted += new FilePostedEventHandler(file_Posted); var hidden = new Hidden { ResponseTitle = "A Hidden Field", Value = "some value" }; // create form and add fields to it var form = new Form(); form.AddFields(description, name, gender, email, sports, states, bio, month, agree, eula, file, hidden); return form; }