The FormHelper allows you to output html input elements using the conventions necessary to use the DataBinder on the server side. Ultimately it allows you do to a bi-directional binding -- if used properly.
Elements generation

Buttons Submit(string)
Button(string)
ButtonElement(string)
Select Select(string,IEnumerable) Text area TextArea(string) Hidden field HiddenField(string) Checkbox field CheckboxField(string)
CreateCheckboxList(string,IEnumerable)
Radio field RadioField(string,object) File upload FileField(string) Text field TextField(string)
TextFieldValue(string, object)
NumberField(string)
NumberFieldValue(string, object)
Password field PasswordField(string)
PasswordNumberField(string)
Labels LabelFor(string,string)
LabelFor(string,string,IDictionary)

FormValidation
The following operations are related to the Form Validation support:

FormTag(IDictionary) and EndFormTag Opens/close the form tag. They are required to use the form validation DisableValidation Disables validation altogether UseWebValidatorProvider Sets a custom Browser validator provider UsePrototypeValidation Configures the helper to use the prototype easy field validation. Must be invoked before FormTag UsefValidate Configures the helper to use the fValidate. Deprecated. UseZebdaValidation Configures the helper to use the Zebda. Must be invoked before FormTag

Mask support.
For most elements, you can use the entries mask and optionally mask_separator to define a mask for your inputs. Kudos to mordechai Sandhaus - 52action.com

For example: mask='2,5',mask_separator='/' will mask the content to '12/34/1234'

Inheritance: AbstractHelper, IServiceEnabledComponent
		public void Init()
		{
			var en = CultureInfo.CreateSpecificCulture("en");

			Thread.CurrentThread.CurrentCulture	= en;
			Thread.CurrentThread.CurrentUICulture = en;

			helper = new FormHelper();

			subscription = new Subscription();
			mock = new MockClass();
			months = new[] {new Month(1, "January"), new Month(1, "February")};
			product = new Product("memory card", 10, (decimal) 12.30);
			user = new SimpleUser();
			users = new[] { new SimpleUser(1, false), new SimpleUser(2, true), new SimpleUser(3, false), new SimpleUser(4, true) };
			mock.Values = new[] { 2, 3 };

			var controller = new HomeController();
			var context = new ControllerContext();

			context.PropertyBag.Add("product", product);
			context.PropertyBag.Add("user", user);
			context.PropertyBag.Add("users", users);
			context.PropertyBag.Add("roles", new[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
			context.PropertyBag.Add("sendemail", true);
			context.PropertyBag.Add("confirmation", "abc");
			context.PropertyBag.Add("fileaccess", FileAccess.Read);
			context.PropertyBag.Add("subscription", subscription);
			context.PropertyBag.Add("months", months);
			context.PropertyBag.Add("mock", mock);

			helper.SetController(controller, context);
		}
コード例 #2
0
        public virtual void SetUp()
        {

            PropertyBag = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
            Helpers = new HelperDictionary();
            var services = new StubMonoRailServices
                               {
                                   UrlBuilder = new DefaultUrlBuilder(new StubServerUtility(), new StubRoutingEngine()),
                                   UrlTokenizer = new DefaultUrlTokenizer()
                               };
            var urlInfo = new UrlInfo(
                "example.org", "test", "/TestBrail", "http", 80,
                "http://test.example.org/test_area/test_controller/test_action.tdd",
                Area, ControllerName, Action, "tdd", "no.idea");
            StubEngineContext = new StubEngineContext(new StubRequest(), new StubResponse(), services, urlInfo);
            StubEngineContext.AddService<IUrlBuilder>(services.UrlBuilder);
            StubEngineContext.AddService<IUrlTokenizer>(services.UrlTokenizer);

            ViewComponentFactory = new DefaultViewComponentFactory();
            ViewComponentFactory.Service(StubEngineContext);
            ViewComponentFactory.Initialize();

            StubEngineContext.AddService<IViewComponentFactory>(ViewComponentFactory);
            ControllerContext = new ControllerContext
                                    {
                                        Helpers = Helpers, 
                                        PropertyBag = PropertyBag
                                    };
            StubEngineContext.CurrentControllerContext = ControllerContext;


            Helpers["urlhelper"] = Helpers["url"] = new UrlHelper(StubEngineContext);
            Helpers["formhelper"] = Helpers["form"] = new FormHelper(StubEngineContext);
            Helpers["dicthelper"] = Helpers["dict"] = new DictHelper(StubEngineContext);
            Helpers["DateFormatHelper"] = Helpers["DateFormat"] = new DateFormatHelper(StubEngineContext);



            var loader = new FileAssemblyViewSourceLoader("Views");
            _monoRailViewEngine = new NHamlMonoRailViewEngine();
            _monoRailViewEngine.TemplateEngine.Options.TemplateCompiler = new CSharp3TemplateCompiler();
            _monoRailViewEngine.SetViewSourceLoader(loader);
            _templateEngine = _monoRailViewEngine.TemplateEngine;
            _templateEngine.Options.TemplateBaseType = typeof( NHamlMonoRailView );
            


            ViewComponentFactory.Inspect(GetType().Assembly);

        }
コード例 #3
0
		public void Init()
		{
			CultureInfo en = CultureInfo.CreateSpecificCulture("en");

			Thread.CurrentThread.CurrentCulture	= en;
			Thread.CurrentThread.CurrentUICulture = en;

			helper = new FormHelper();
			model = new ModelWithValidation();

			HomeController controller = new HomeController();
			PrepareController(controller, "", "Home", "Index");

			controller.PropertyBag.Add("model", model);

			helper.SetController(controller);
		}
コード例 #4
0
        public override void Render()
        {
            var writer = new StringWriter();
            formHelper = (FormHelper) EngineContext.CurrentControllerContext.Helpers["Form"];
            formHelper.UseJQueryValidation();
            foreach (var field in Fields)
            {
                // Render Start Row
                renderRowSection(STARTROWSECTION, writer, DEFAULTSTARTROW);
                switch(field.Type)
                {
                    case FieldType.SingleLineTextField:
                        renderSingleLineTextField(field, writer);
                        break;
                    case FieldType.MultiLineTextField:
                        renderMultiLineTextField(field, writer);
                        break;
                    case FieldType.TextEditor:
                        renderMultiLineTextField(field, writer, TEXTEDITORCLASS);
                        break;
                    case FieldType.Hidden:
                        renderHiddenField(field, writer);
                        break;
                    case FieldType.SingleSelectDropDownList:
                        renderSelectList(field, writer, false);
                        break;
                    case FieldType.MultiSelectDropDownList:
                        renderSelectList(field, writer, true);
                        break;
                    case FieldType.CheckBox:
                        renderCheckBox(field, writer);
                        break;
                    case FieldType.Date:
                        renderDateField(field, writer);
                        break;
                    default:
                        throw new NoRenderMethodForFormFieldType();

                }

                // Render End Row
                renderRowSection(ENDROWSECTION, writer, DEFAULTENDROW);
            }

            RenderText(writer.ToString());
        }
コード例 #5
0
		public void Init()
		{
			CultureInfo en = CultureInfo.CreateSpecificCulture("en");

			Thread.CurrentThread.CurrentCulture	= en;
			Thread.CurrentThread.CurrentUICulture = en;

			helper = new FormHelper();

			subscription = new Subscription();
			months = new Month[] {new Month(1, "January"), new Month(1, "February")};
			product = new Product("memory card", 10, (decimal) 12.30);
			user = new SimpleUser();
			contact = new Contact();

			HomeController controller = new HomeController();
			ControllerContext context = new ControllerContext();

			context.PropertyBag.Add("product", product);
			context.PropertyBag.Add("user", user);
			context.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
			context.PropertyBag.Add("sendemail", true);
			context.PropertyBag.Add("confirmation", "abc");
			context.PropertyBag.Add("fileaccess", FileAccess.Read);
			context.PropertyBag.Add("subscription", subscription);
			context.PropertyBag.Add("months", months);
			context.PropertyBag.Add("contact", contact);

			workTable = new DataTable("Customers");
			DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
			workCol.AllowDBNull = false;
			workCol.Unique = true;
			workTable.Columns.Add("Name", typeof(String));

			DataRow row = workTable.NewRow();
			row[0] = 1;
			row[1] = "chris rocks";
			workTable.Rows.Add(row);
			row = workTable.NewRow();
			row[0] = 2;
			row[1] = "will ferrell";
			workTable.Rows.Add(row);

			helper.SetController(controller, context);
		}
コード例 #6
0
ファイル: FormHelper.cs プロジェクト: rambo-returns/MonoRail
			/// <summary>
			/// Initializes a new instance of the <see cref="CheckboxList"/> class.
			/// </summary>
			/// <param name="helper">The helper.</param>
			/// <param name="target">The object to get the value from and to be based on to create the element name.</param>
			/// <param name="initialSelectionSet">The initial selection set.</param>
			/// <param name="dataSource">The set of available elements</param>
			/// <param name="attributes">Attributes for the FormHelper method and for the html element it generates</param>
			public CheckboxList(FormHelper helper, string target,
								object initialSelectionSet, IEnumerable dataSource, IDictionary attributes)
			{
				if (dataSource == null) throw new ArgumentNullException("dataSource");

				this.helper = helper;
				this.target = target;
				this.attributes = attributes ?? new HybridDictionary(true);

				operationState = SetOperation.IterateOnDataSource(initialSelectionSet, dataSource, attributes);
				enumerator = operationState.GetEnumerator();
			}
コード例 #7
0
		public void SetUp()
		{
			Layout = null;
			PropertyBag = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
			Helpers = new HelperDictionary();
			StubMonoRailServices services = new StubMonoRailServices();
			services.UrlBuilder = new DefaultUrlBuilder(new StubServerUtility(), new StubRoutingEngine());
			services.UrlTokenizer = new DefaultUrlTokenizer();
			UrlInfo urlInfo = new UrlInfo(
				"example.org", "test", "/TestBrail", "http", 80,
				"http://test.example.org/test_area/test_controller/test_action.tdd",
				Area, ControllerName, Action, "tdd", "no.idea");
			StubEngineContext = new StubEngineContext(new StubRequest(), new StubResponse(), services,
													  urlInfo);
			StubEngineContext.AddService<IUrlBuilder>(services.UrlBuilder);
			StubEngineContext.AddService<IUrlTokenizer>(services.UrlTokenizer);

			ViewComponentFactory = new DefaultViewComponentFactory();
			ViewComponentFactory.Service(StubEngineContext);
			ViewComponentFactory.Initialize();

			StubEngineContext.AddService<IViewComponentFactory>(ViewComponentFactory);
			ControllerContext = new ControllerContext();
			ControllerContext.Helpers = Helpers;
			ControllerContext.PropertyBag = PropertyBag;
			StubEngineContext.CurrentControllerContext = ControllerContext;


			Helpers["formhelper"] = Helpers["form"] = new FormHelper(StubEngineContext);
			Helpers["urlhelper"] = Helpers["url"] = new UrlHelper(StubEngineContext);
			Helpers["dicthelper"] = Helpers["dict"] = new DictHelper(StubEngineContext);
			Helpers["DateFormatHelper"] = Helpers["DateFormat"] = new DateFormatHelper(StubEngineContext);

			string viewPath = Path.Combine(viewSourcePath, "Views");

			FileAssemblyViewSourceLoader loader = new FileAssemblyViewSourceLoader(viewPath);
			loader.AddAssemblySource(
				new AssemblySourceInfo(Assembly.GetExecutingAssembly().FullName,
									   "Castle.MonoRail.Views.Brail.Tests.ResourcedViews"));

			BooViewEngine = new BooViewEngine();
			BooViewEngine.Options = new BooViewEngineOptions();
			BooViewEngine.Options.SaveDirectory = Environment.CurrentDirectory;
			BooViewEngine.Options.SaveToDisk = false;
			BooViewEngine.Options.Debug = true;
			BooViewEngine.Options.BatchCompile = false;

			BooViewEngine.SetViewSourceLoader(loader);
			BooViewEngine.Initialize();

			BeforEachTest();
		}
 private void RenderItem(FormHelper.CheckboxList list, object item, int index)
 {
     PropertyBag["item"] = item;
     RenderText(list.Item());
     string checkboxId = string.Format("{0}_{1}_", target.Replace('.', '_'), index);
     RenderLabel(item, checkboxId);
 }
        private void RenderItemsInColumns(FormHelper.CheckboxList list)
        {
            int itemCount = GetCount(source);
			int itemsPerColumn = ((itemCount + columns - 1) / columns);
            int index = 0;
            int positionInColumn = 1;
            RenderText("<table><tr>");
            foreach (object item in list)
            {
                if (positionInColumn == 1)
                {
                    RenderTextFormat("<td style='vertical-align:{0};'>", columnVerticalAlign);
                }
                RenderItemStart();
                RenderItem(list, item, index);
                RenderItemEnd();
                index++;
                if (positionInColumn == itemsPerColumn || index == itemCount)
                {
                    RenderText("</td>\n");
                    positionInColumn = 1;
                }
                else
                {
                    positionInColumn++;
                }
            }
            RenderText("</tr></table>\n");
        }