Skip to content

Instantly share code, notes, and snippets.

@ryankelley
Created March 9, 2010 19:09
Show Gist options
  • Save ryankelley/326963 to your computer and use it in GitHub Desktop.
Save ryankelley/326963 to your computer and use it in GitHub Desktop.
public class KokugenHtmlConventions : HtmlConventionRegistry
{
public KokugenHtmlConventions()
{
numbers();
validationAttributes();
editors();
Editors.Builder<FormItemBuilder>();
}
private void editors()
{
Editors.IfPropertyIs<bool>().BuildBy(request => new CheckboxTag(request.Value<bool>()).Style("width", "auto !important").Attr("value", request.ElementId));
Editors.Builder(new FormItemBuilder());
}
private void numbers()
{
Editors.IfPropertyIs<Int32>().Attr("max", Int32.MaxValue);
Editors.IfPropertyIs<Int16>().Attr("max", Int16.MaxValue);
Editors.IfPropertyIs<Int64>().Attr("max", Int64.MaxValue);
Editors.IfPropertyTypeIs(IsIntegerBased).AddClass("integer");
Editors.IfPropertyTypeIs(IsFloatingPoint).AddClass("number");
}
// Declare policies for using validation attributes
private void validationAttributes()
{
Editors.AddClassForAttribute<RequiredAttribute>("required");
Editors.ModifyForAttribute<MaximumStringLengthAttribute>((tag, att) =>
{
if (att.Length <Entity.UnboundedStringLength)
{
tag.Attr("maxlength", att.Length);
}
});
Editors.ModifyForAttribute<GreaterOrEqualToZeroAttribute>(tag => tag.Attr("min", 0));
Editors.ModifyForAttribute<GreaterThanZeroAttribute>(tag => tag.Attr("min", 1));
}
private static bool IsIntegerBased(Type type)
{
return type == typeof(int) || type == typeof(long) || type == typeof(short);
}
private static bool IsFloatingPoint(Type type)
{
return type == typeof(decimal) || type == typeof(float) || type == typeof(double);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment