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 _errors; #endregion #region FormTag public override string FormTag(IDictionary attributes) { if (Controller.Flash.ContainsKey("errors")) { _errors = (Dictionary) Controller.Flash["errors"]; } return FormTag(UrlHelper.For(attributes), attributes); } public override string EndFormTag() { return ""; } #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}{1}", 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}{1}", 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}{1}", TextAreaValue(name, value, attributes), _errors[name] ); } } return TextAreaValue(name, value, attributes); } #endregion } }