-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example.fan
95 lines (77 loc) · 3.02 KB
/
Example.fan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using afIoc
using afBedSheet
using afFormBean
class ContactUsPage {
@Inject
HttpRequest httpRequest
@Inject { type=ContactDetails# }
FormBean formBean
new make(|This|in) { in(this) }
Text render() {
Text.fromHtml(
"<!DOCTYPE html>
<html>
<head>
<title>FormBean Demo</title>
<link rel='stylesheet' href='/styles.css' >
</head>
<body>
<h2>Contact Us</h2>
<span class='requiredNotification'>* Denotes Required Field</span>
<form class='contactForm' action='/contact' method='POST'>
${ formBean.renderErrors() }
${ formBean.renderBean(null) }
${ formBean.renderSubmit() }
</form>
</body>
</html>")
}
Text onContact() {
// perform server side validation
// if invalid, re-render the page and show the errors
if (!formBean.validateForm(httpRequest.body.form))
return render
// create an instance of our form object
contactDetails := (ContactDetails) formBean.createBean
echo("Contact made!")
echo(" - name: ${contactDetails.name}")
echo(" - email: ${contactDetails.email}")
echo(" - website: ${contactDetails.website}")
echo(" - message: ${contactDetails.message}")
// display a simple message
return Text.fromPlain("Thank you ${contactDetails.name}, we'll be in touch.")
}
}
class ContactDetails {
@HtmlInput { required=true; placeholder="Fred Bloggs"; attributes="autofocus" }
Str name
@HtmlInput { type="email"; required=true; placeholder="fred.bloggs@example.com"; hint="Proper format 'name@something.com'" }
Uri email
@HtmlInput { type="url"; required=true; placeholder="http://www.example.com"; hint="Proper format 'http://someaddress.com'" }
Str website
@HtmlInput { type="textarea"; required=true; attributes="rows='6'"}
Str message
new make(|This|in) { in(this) }
@Validate { field=#name }
static Void validateName(FormField formField) {
if (formField.formValue == "Trisha")
formField.errMsg = "Ex-girlfriends not allowed!"
}
}
// @SubModule only needed because this example is run as a script
@SubModule { modules=[FormBeanModule#] }
const class AppModule {
@Contribute { serviceType=Routes# }
Void contributeRoutes(Configuration conf) {
conf.add(Route(`/`, ContactUsPage#render))
conf.add(Route(`/contact`, ContactUsPage#onContact, "POST"))
// to save you typing in a stylesheet, we'll just redirect to one I made earlier
// conf.add(Route(`/styles.css`, `styles.css`.toFile))
conf.add(Route(`/styles.css`, Redirect.movedTemporarily(`http://static.alienfactory.co.uk/fantom-docs/afFormBean-quickStart.css`)))
}
}
class Main {
Int main() {
BedSheetBuilder("Example_0").startWisp(8089)
}
}