Removed extra indentations in code.

This commit is contained in:
2017-02-14 19:51:23 +01:00
parent f216e98430
commit ff8da87fe3

View File

@@ -24,89 +24,88 @@ A simple solution to return html content is to use the `WSF_HTML_PAGE_RESPONSE`
In a `WSF_EXECUTION` descendant, see how to implement the `execute` procedure. In a `WSF_EXECUTION` descendant, see how to implement the `execute` procedure.
``` ```
execute execute
local local
mesg: WSF_HTML_PAGE_RESPONSE mesg: WSF_HTML_PAGE_RESPONSE
do do
create mesg.make create mesg.make
mesg.set_title ("This is an HTML page") mesg.set_title ("This is an HTML page")
mesg.set_body ("<h1>HTML Response</h1>") mesg.set_body ("<h1>HTML Response</h1>")
response.send (mesg) response.send (mesg)
end end
``` ```
In this case, the html is hardcoded, and written manually. In this case, the html is hardcoded, and written manually.
Now let's see how to use the WSF widgets on a more advanced usecase, a simple sign-in web form with 2 input fields `username`, `password` and `submit` button. Now let's see how to use the WSF widgets on a more advanced usecase, a simple sign-in web form with 2 input fields `username`, `password` and `submit` button.
``` ```
execute
do
-- Basic url dispatching...
if request.is_post_request_method then
execute_web_form_submission
else
execute_web_form_display
end
end
execute execute_web_form_display
do local
-- Basic url dispatching... mesg: WSF_HTML_PAGE_RESPONSE
if request.is_post_request_method then f: WSF_FORM
execute_web_form_submission l_html: STRING
else do
execute_web_form_display -- Build the web form
end f := new_login_form
end
execute_web_form_display -- Html generation
local --| the first argument of `to_html` is the theme for advanced usage you can provide a custom theme
mesg: WSF_HTML_PAGE_RESPONSE --| that can redefine how to generate link for instance.
f: WSF_FORM l_html := f.to_html (create {WSF_REQUEST_THEME}.make_with_request (request))
l_html: STRING
do
-- Build the web form
f := new_login_form
-- Html generation -- Send the response
--| the first argument of `to_html` is the theme for advanced usage you can provide a custom theme create mesg.make
--| that can redefine how to generate link for instance. mesg.set_title ("This is a Web form")
l_html := f.to_html (create {WSF_REQUEST_THEME}.make_with_request (request)) mesg.set_body (l_html)
response.send (mesg)
end
-- Send the response execute_web_form_submission
create mesg.make do
mesg.set_title ("This is a Web form") ... this will be explain in next section !
mesg.set_body (l_html) end
response.send (mesg)
end
execute_web_form_submission new_login_form: WSF_FORM
do local
... this will be explain in next section ! f: WSF_FORM
end f_set: WSF_FORM_FIELD_SET
f_username: WSF_FORM_TEXT_INPUT
f_password: WSF_FORM_PASSWORD_INPUT
f_submit: WSF_FORM_SUBMIT_INPUT
do
-- Build the web form
create f.make ("/form", "form-login") -- The form id is optional.
f.set_method_post -- it could also use the default GET method.
new_login_form: WSF_FORM -- Put specific html code
local f.extend_html_text ("<h1>Web form example</h1>")
f: WSF_FORM
f_set: WSF_FORM_FIELD_SET
f_username: WSF_FORM_TEXT_INPUT
f_password: WSF_FORM_PASSWORD_INPUT
f_submit: WSF_FORM_SUBMIT_INPUT
do
-- Build the web form
create f.make ("/form", "form-login") -- The form id is optional.
f.set_method_post -- it could also use the default GET method.
-- Put specific html code -- username input field
f.extend_html_text ("<h1>Web form example</h1>") create f_username.make ("username")
f_username.set_label ("User name")
f.extend (f_username)
-- username input field -- password input field
create f_username.make ("username") create f_password.make ("password")
f_username.set_label ("User name") f_password.set_label ("Password")
f.extend (f_username) f.extend (f_password)
-- password input field -- submit button
create f_password.make ("password") create f_submit.make_with_text ("op", "Login")
f_password.set_label ("Password") f.extend (f_submit)
f.extend (f_password)
-- submit button Result := f
create f_submit.make_with_text ("op", "Login") end
f.extend (f_submit)
Result := f
end
``` ```
## Handling web form data ## Handling web form data
@@ -120,63 +119,63 @@ Indeed `WSF_HTML.process (request, .., ..)` analyze the request, fill the form f
See See
``` ```
process (req: WSF_REQUEST; a_before_callback, a_after_callback: detachable PROCEDURE [WSF_FORM_DATA]) process (req: WSF_REQUEST; a_before_callback, a_after_callback: detachable PROCEDURE [WSF_FORM_DATA])
-- Process Current form with request `req`, -- Process Current form with request `req`,
-- and build `last_data: WSF_FORM_DATA` object containing the field values. -- and build `last_data: WSF_FORM_DATA` object containing the field values.
-- agent `a_before_callback` is called before the validation -- agent `a_before_callback` is called before the validation
-- agent `a_after_callback` is called after the validation -- agent `a_after_callback` is called after the validation
``` ```
In our previous sample code, `execute_web_form_submission` could be: In our previous sample code, `execute_web_form_submission` could be:
``` ```
execute_web_form_submission execute_web_form_submission
local local
mesg: WSF_HTML_PAGE_RESPONSE mesg: WSF_HTML_PAGE_RESPONSE
l_html: STRING l_html: STRING
f: WSF_FORM f: WSF_FORM
do do
create mesg.make create mesg.make
mesg.set_title ("Web form submission") mesg.set_title ("Web form submission")
create l_html.make_empty create l_html.make_empty
-- Build again the same form. -- Build again the same form.
f := new_login_form f := new_login_form
-- Process this form with `request` data. -- Process this form with `request` data.
f.process (request, Void, Void) f.process (request, Void, Void)
if attached f.last_data as form_data and then not form_data.has_error then if attached f.last_data as form_data and then not form_data.has_error then
-- `form_data` contains the submitted fields with their value. -- `form_data` contains the submitted fields with their value.
-- Depending on the form data, display a response. -- Depending on the form data, display a response.
l_html.append ("Username: ") l_html.append ("Username: ")
if attached form_data.string_item ("username") as l_username then if attached form_data.string_item ("username") as l_username then
-- The username may contain unicode, or characters like '<' -- The username may contain unicode, or characters like '<'
-- thus, it needs to be html encoded -- thus, it needs to be html encoded
l_html.append (mesg.html_encoded_string (l_username)) l_html.append (mesg.html_encoded_string (l_username))
else else
l_html.append ("missing value !!") l_html.append ("missing value !!")
end end
l_html.append ("<br/>") l_html.append ("<br/>")
if attached form_data.string_item ("password") as l_password then if attached form_data.string_item ("password") as l_password then
l_html.append ("Password: ") l_html.append ("Password: ")
-- The password may contain unicode, or characters like '<' -- The password may contain unicode, or characters like '<'
-- thus, it needs to be html encoded -- thus, it needs to be html encoded
l_html.append (mesg.html_encoded_string (l_password)) l_html.append (mesg.html_encoded_string (l_password))
else else
l_html.append ("missing value !!") l_html.append ("missing value !!")
end end
l_html.append ("<br/>") l_html.append ("<br/>")
else else
l_html.append ("Error while processing the web form!") l_html.append ("Error while processing the web form!")
-- Display again the form (it will contain the submitted values, if any). -- Display again the form (it will contain the submitted values, if any).
f.append_to_html (create {WSF_REQUEST_THEME}.make_with_request (request), l_html) f.append_to_html (create {WSF_REQUEST_THEME}.make_with_request (request), l_html)
end end
mesg.set_body (l_html) mesg.set_body (l_html)
response.send (mesg) response.send (mesg)
end end
``` ```
In this case, the code could report an error if the username is empty, or with unwanted character ... this could be done in the `execute_web_form_submission` code, but it is also possible to set validation on each form field. In this case, the code could report an error if the username is empty, or with unwanted character ... this could be done in the `execute_web_form_submission` code, but it is also possible to set validation on each form field.
@@ -186,19 +185,19 @@ To set validation,
For instance, in previous sample, accepts only alpha-numeric characters: For instance, in previous sample, accepts only alpha-numeric characters:
``` ```
f_username.set_description ("Only alpha-numeric character are accepted.") f_username.set_description ("Only alpha-numeric character are accepted.")
f_username.set_validation_action (agent (fd: WSF_FORM_DATA) f_username.set_validation_action (agent (fd: WSF_FORM_DATA)
do do
if attached fd.string_item ("username") as u then if attached fd.string_item ("username") as u then
if across u as ic some not ic.item.is_alpha_numeric end then if across u as ic some not ic.item.is_alpha_numeric end then
fd.report_invalid_field ("username", "Missing username value!") fd.report_invalid_field ("username", "Missing username value!")
elseif u.is_whitespace then elseif u.is_whitespace then
fd.report_invalid_field ("username", "Empty username value!") fd.report_invalid_field ("username", "Empty username value!")
end end
else else
fd.report_invalid_field ("username", "Missing username value!") fd.report_invalid_field ("username", "Missing username value!")
end end
end) end)
``` ```
Notes: Notes:
@@ -237,4 +236,3 @@ For now, the `wsf_html` provides a simple solution to build web form. It is alwa
Advanced usage could be built on top of the `wsf_html` to include for instance javascript actions, but this is out of the scope of `wsf_html` . Advanced usage could be built on top of the `wsf_html` to include for instance javascript actions, but this is out of the scope of `wsf_html` .