22
33To use WebForms Core, first copy the WebForms class file in this directory to your project. Then create a new View file similar to the one below.
44
5+ Create a template file "default.html.eex" in the "templates/my" directory.
6+
57View file
68``` html
79<!DOCTYPE html>
@@ -11,20 +13,18 @@ View file
1113 <script type =" text/javascript" src =" /script/web-forms.js" ></script >
1214</head >
1315<body >
14- <form method =" post" action =" <%= Routes.form_path(@conn, :create) %>" >
15-
16- <label for =" txt_Name" >Your Name</label >
17- <input name =" txt_Name" id =" txt_Name" type =" text" />
18- <br >
19- <label for =" txt_FontSize" >Set Font Size</label >
20- <input name =" txt_FontSize" id =" txt_FontSize" type =" number" value =" 16" min =" 10" max =" 36" />
21- <br >
22- <label for =" txt_BackgroundColor" >Set Background Color</label >
23- <input name =" txt_BackgroundColor" id =" txt_BackgroundColor" type =" text" />
24- <br >
25- <input name =" btn_SetBodyValue" type =" submit" value =" Click to send data" />
26-
27- </form >
16+ <form method =" post" action =" /" >
17+ <label for =" txt_Name" >Your Name</label >
18+ <input name =" txt_Name" id =" txt_Name" type =" text" />
19+ <br >
20+ <label for =" txt_FontSize" >Set Font Size</label >
21+ <input name =" txt_FontSize" id =" txt_FontSize" type =" number" value =" 16" min =" 10" max =" 36" />
22+ <br >
23+ <label for =" txt_BackgroundColor" >Set Background Color</label >
24+ <input name =" txt_BackgroundColor" id =" txt_BackgroundColor" type =" text" />
25+ <br >
26+ <input name =" btn_SetBodyValue" type =" submit" value =" Click to send data" />
27+ </form >
2828</body >
2929</html >
3030```
@@ -33,33 +33,45 @@ Also, create a Controller class file as follows.
3333
3434Controller class
3535``` elixir
36- defmodule MyAppWeb .FormController do
36+ defmodule MyAppWeb .MyController do
3737 use MyAppWeb , :controller
3838
39- alias MyApp .WebForms
40-
39+ # GET request handler
4140 def index (conn, _params ) do
42- render (conn, " index .html" )
41+ render (conn, " default .html" )
4342 end
4443
45- def create (conn, %{" txt_Name" => name, " txt_BackgroundColor" => background_color, " txt_FontSize" => font_size}) do
46- font_size = String .to_integer (font_size)
44+ # POST request handler
45+ def submit (conn, %{
46+ " txt_Name" => name,
47+ " txt_BackgroundColor" => background_color,
48+ " txt_FontSize" => font_size,
49+ " btn_SetBodyValue" => _button
50+ }) do
51+
52+ form = WebForms .new ()
53+ form = WebForms .set_font_size (form, InputPlace .tag (" form" ), font_size)
54+ form = WebForms .set_background_color (form, InputPlace .tag (" form" ), background_color)
55+ form = WebForms .set_disabled (form, InputPlace .name (" btn_SetBodyValue" ), true )
56+ form = WebForms .add_tag (form, InputPlace .tag (" form" ), " h3" , nil )
57+ form = WebForms .set_text (form, InputPlace .tag (" h3" ), " Welcome #{ name } !" )
4758
48- form = %WebForms {}
59+ response = WebForms .response (form)
60+ text (conn, response)
61+ end
62+ end
63+ ```
4964
50- form =
51- form
52- |> WebForms .set_font_size (InputPlace .tag (" form" ), font_size)
53- |> WebForms .set_background_color (InputPlace .tag (" form" ), background_color)
54- |> WebForms .set_disabled (InputPlace .name (" btn_SetBodyValue" ), true )
55- |> WebForms .add_tag (InputPlace .tag (" form" ), " h3" )
56- |> WebForms .set_text (InputPlace .tag (" h3" ), " Welcome #{ name } !" )
65+ Add routes for the controller in the Phoenix router.
5766
58- response = WebForms .response (form)
67+ Router (router.ex)
68+ ``` elixir
69+ defmodule MyAppWeb .Router do
70+ use MyAppWeb , :router
5971
60- conn
61- |> put_flash ( :info , response)
62- |> redirect ( to: " /" )
72+ scope " / " , MyAppWeb do
73+ get " / " , MyController , :index
74+ post " /" , MyController , :submit
6375 end
6476end
6577```
@@ -71,4 +83,4 @@ As you can see, the WebFormsJS script has been added in the header section of th
7183
7284The latest version of the WebFormsJS script is available through the link below.
7385
74- https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js
86+ https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js
0 commit comments