All pastes #818316 Raw Edit

Something

public text v1 · immutable
·published 2007-12-16 06:23 UTC
rendered paste body
namespace NetCoders.Portal.Core.Helpers
{
	using System.Collections;
	using System.Collections.Generic;
	using Castle.MonoRail.Framework.Helpers;

	public class HtmlFormHelper : FormHelper
	{
		#region Fields
		private Dictionary<string, string> _errors;
		#endregion

		#region FormTag
		public override string FormTag(IDictionary attributes)
		{
			if (Controller.Flash.ContainsKey("errors"))
			{
				_errors = (Dictionary<string, string>) Controller.Flash["errors"];
			}

			return FormTag(UrlHelper.For(attributes), attributes);
		}

		public override string EndFormTag()
		{
			return "</form>";
		}
		#endregion

		#region TextField
		public override string TextField(string name)
		{
			return TextField(name, null);
		}

		public override string TextField(string name, IDictionary attributes)
		{
			name = RewriteTargetIfWithinObjectScope(name);

			object value = ObtainValue(name);

			if (_errors != null)
			{
				if (_errors.ContainsKey(name))
				{
					if (attributes != null)
					{
						if (attributes.Contains("class"))
						{
							attributes.Remove("class");
						}

						attributes.Add("class", "error");
					}
					else
					{
						attributes = DictHelper.Create("class=error");
					}

					return string.Format(
						"{0}<span class=\"error\">{1}</span>", 
						CreateInputElement("text", name, value, attributes), 
						_errors[name]
					);
				}
			}

			return CreateInputElement("text", name, value, attributes);
		}
		#endregion

		#region Select
		public override string Select(string name, IEnumerable items)
		{
			return Select(name, items, null);
		}

		public override string Select(string name, IEnumerable items, IDictionary attributes)
		{
			name = RewriteTargetIfWithinObjectScope(name);

			object value = ObtainValue(name);

			if (_errors != null)
			{
				if (_errors.ContainsKey(name))
				{
					if (attributes != null)
					{
						if (attributes.Contains("class"))
						{
							attributes.Remove("class");
						}

						attributes.Add("class", "error");
					}
					else
					{
						attributes = DictHelper.Create("class=error");
					}

					return string.Format(
						"{0}<span class=\"error\">{1}</span>",
						Select(name, value, items, attributes),
						_errors[name]
					);
				}
			}

			return Select(name, value, items, attributes);
		}
		#endregion

		#region TextArea
		public override string TextArea(string name)
		{
			return TextArea(name, null);
		}

		public override string TextArea(string name, IDictionary attributes)
		{
			string targetValue = RewriteTargetIfWithinObjectScope(name);

			object value = ObtainValue(targetValue);

			if (_errors != null)
			{
				if (_errors.ContainsKey(name))
				{
					if (attributes != null)
					{
						if (attributes.Contains("class"))
						{
							attributes.Remove("class");
						}

						attributes.Add("class", "error");
					}
					else
					{
						attributes = DictHelper.Create("class=error");
					}

					return string.Format(
						"{0}<span class=\"error\">{1}</span>",
						TextAreaValue(name, value, attributes),
						_errors[name]
					);
				}
			}

			return TextAreaValue(name, value, attributes);
		}
		#endregion
	}
}