Skip to content

Instantly share code, notes, and snippets.

@forcewake
Last active August 29, 2015 13:56
Show Gist options
  • Save forcewake/8898327 to your computer and use it in GitHub Desktop.
Save forcewake/8898327 to your computer and use it in GitHub Desktop.
Dynamic form from c# to jquery dynamic form
{
"action": "index.html",
"method": "get",
"html": [
{
"type": "p",
"html": "You must login"
},
{
"type": "text",
"name": "username",
"id": "txt-username",
"caption": "Username",
"placeholder": "E.g. [email protected]"
},
{
"type": "password",
"name": "password",
"caption": "Password"
},
{
"type": "submit",
"value": "login"
}
]
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Stateless;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
FormObject form = new FormObject
{
Action = "index.html",
Method = FormMethod.Get,
Html = new List<HtmlElement>
{
new HtmlElementImpl
{
Html = "test"
},
new HtmlElement
{
Name = "username",
Id = "txt-username",
Caption = "Username",
Type = ElementType.Text,
Placeholder = "E.g. [email protected]",
ValidateInfo = new ValidateInfo
{
IsRequired = true,
MinLength = 10,
RequiredMessage = new RequiredMessage
{
Text = "Password!"
}
}
},
new HtmlElement
{
Name = "password",
Caption = "Password",
Type = ElementType.Password,
},
new HtmlElement
{
Type = ElementType.Submit,
Value = "login"
}
}
};
string serializeObject = JsonConvert.SerializeObject(form, Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Console.WriteLine(serializeObject);
Config config = new Config();
config.Name("Foo")
.Elements(e =>
{
e.Name("element1").Height(23);
e.Name("element2").Height(31);
})
.Foo(3232);
Console.WriteLine("Press any key...");
Console.ReadKey(true);
}
}
public class EnumTypeConverter<T> : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
T formMethod = (T) value;
writer.WriteValue(formMethod.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
return (T) Activator.CreateInstance(typeof (T), new object[] {reader.Value});
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof (T);
}
}
public class RequiredMessage
{
[JsonProperty("required")]
public string Text { get; set; }
}
public class ValidateInfo
{
[JsonProperty("required")]
public bool IsRequired { get; set; }
[JsonProperty("minlength")]
public int MinLength { get; set; }
[JsonProperty("messages")]
public RequiredMessage RequiredMessage { get; set; }
}
public class HtmlElement
{
[JsonProperty("type")]
[JsonConverter(typeof (EnumTypeConverter<ElementType>))]
public virtual ElementType Type { get; set; }
[JsonProperty("html")]
public virtual List<HtmlElement> Html { get; set; }
[JsonProperty("name")]
public virtual string Name { get; set; }
[JsonProperty("id")]
public virtual string Id { get; set; }
[JsonProperty("caption")]
public virtual string Caption { get; set; }
[JsonProperty("placeholder")]
public virtual string Placeholder { get; set; }
[JsonProperty("value")]
public virtual string Value { get; set; }
[JsonProperty("validate")]
public virtual ValidateInfo ValidateInfo { get; set; }
public HtmlElement()
{
this.Type = ElementType.Div;
}
}
public class HtmlElementImpl : HtmlElement
{
public override ElementType Type { get; set; }
[JsonProperty("html")]
public new string Html { get; set; }
public override string Name { get; set; }
public override string Id { get; set; }
public override string Caption { get; set; }
public override string Placeholder { get; set; }
public override string Value { get; set; }
public override ValidateInfo ValidateInfo { get; set; }
}
public class FormObject
{
[JsonProperty("action")]
public string Action { get; set; }
[JsonProperty("method")]
[JsonConverter(typeof (EnumTypeConverter<FormMethod>))]
public FormMethod Method { get; set; }
[JsonProperty("html")]
public List<HtmlElement> Html { get; set; }
}
public sealed class FormMethod : EnumType<string>
{
public static readonly FormMethod Get = new FormMethod("get");
public static readonly FormMethod Post = new FormMethod("post");
public FormMethod(string value) : base(value)
{
}
}
public class EnumType<T>
{
public EnumType(T value)
{
this.Value = value;
}
public override string ToString()
{
return this.Value.ToString();
}
public static implicit operator string(EnumType<T> op)
{
return op.Value.ToString();
}
public T Value { get; private set; }
}
public sealed class ElementType : EnumType<string>
{
public static readonly ElementType Container = new ElementType("container");
public static readonly ElementType P = new ElementType("p");
public static readonly ElementType Div = new ElementType("div");
public static readonly ElementType Text = new ElementType("text");
public static readonly ElementType Password = new ElementType("password");
public static readonly ElementType Submit = new ElementType("submit");
public static readonly ElementType Reset = new ElementType("reset");
public static readonly ElementType Hidden = new ElementType("hidden");
public static readonly ElementType File = new ElementType("file");
public static readonly ElementType Radio = new ElementType("radio");
public static readonly ElementType Checkbox = new ElementType("checkbox");
public static readonly ElementType Radiobuttons = new ElementType("radiobuttons");
public static readonly ElementType Checkboxes = new ElementType("checkboxes");
public static readonly ElementType Number = new ElementType("number");
public static readonly ElementType Url = new ElementType("url");
public static readonly ElementType Tel = new ElementType("tel");
public static readonly ElementType Email = new ElementType("email");
public ElementType(string value)
: base(value)
{
}
}
public class Config
{
private string name;
private int foo;
private IList<Element> elements = new List<Element>();
public Config Name(string name)
{
this.name = name;
return this;
}
public Config Foo(int x)
{
this.foo = x;
return this;
}
public Config Elements(Action<ElementBuilder> builderAction)
{
ElementBuilder builder = new ElementBuilder(this);
builderAction(builder);
return this;
}
public class ElementBuilder
{
private readonly Config config;
internal ElementBuilder(Config config)
{
this.config = config;
}
public ElementHeightBuilder Name(string name)
{
Element element = new Element {Name = name};
config.elements.Add(element);
return new ElementHeightBuilder(element);
}
}
public class ElementHeightBuilder
{
private readonly Element element;
internal ElementHeightBuilder(Element element)
{
this.element = element;
}
public void Height(int height)
{
element.Height = height;
}
}
public class Element
{
public string Name { get; set; }
public int Height { get; set; }
}
}
}
{
"action":"index.html",
"method":"post",
"html":[
{
"type":"fieldset",
"caption":"User information",
"html":[
{
"name":"email",
"caption":"Email address",
"type":"text",
"placeholder":"E.g. [email protected]",
"validate":{
"email":true
}
},
{
"name":"password",
"caption":"Password",
"type":"password",
"id":"registration-password",
"validate":{
"required":true,
"minlength":5,
"messages":{
"required":"Please enter a password",
"minlength":"At least {0} characters long"
}
}
},
{
"name":"password-repeat",
"caption":"Repeat password",
"type":"password",
"validate":{
"equalTo":"#registration-password",
"messages":{
"equalTo":"Please repeat your password"
}
}
},
{
"type":"radiobuttons",
"caption":"Sex",
"name":"sex",
"class":"labellist",
"options":{
"f":"Female",
"m":"Male"
}
},
{
"type":"checkboxes",
"name":"test",
"caption":"Receive newsletter about",
"class":"labellist",
"options":{
"updates":"Product updates",
"errors":{
"value":"security",
"caption":"Security warnings",
"checked":"checked"
}
}
}
]
},
{
"type":"fieldset",
"caption":"Address information",
"html":[
{
"name":"name",
"caption":"Your name",
"type":"text",
"placeholder":"E.g. John Doe"
},
{
"name":"address",
"caption":"Address",
"type":"text",
"validate":{ "required":true }
},
{
"name":"zip",
"caption":"ZIP code",
"type":"text",
"size":5,
"validate":{ "required":true }
},
{
"name":"city",
"caption":"City",
"type":"text",
"validate":{ "required":true }
},
{
"type":"select",
"name":"continent",
"caption":"Choose a continent",
"options":{
"america":"America",
"europe":{
"selected":"true",
"id":"europe-option",
"value":"europe",
"html":"Europe"
},
"asia":"Asia",
"africa":"Africa",
"australia":"Australia"
}
}
]
},
{
"type":"submit",
"value":"Signup"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment