Overview of this Example:
The following code is a slimmed down and slightly modified version of the example provided in the default WebSharper-1.0 template. Additionally, this example saves the information captured on the form to a CouchDB database. Note: To run this example, you will need to download and install the WebSharper Platform 2010 Standard - Version 1.0.28 - Public Release (Instructions can be found at http://www.intellifactory.com/products/wsp/GettingStarted.aspx). Additionally, in order for the registration information to be saved to the CouchDB database, CouchDB must be running on the default port (5984) of the local machine and a database named registration must exist.
Setting up the Form:
You probably noticed the input and inputEmail function calls in the form setup. These functions create the form elements with appropriate validation. The code is as follows:
In this simple example, the user completes the three fields and clicks submit. The information is then saved to couch and the user is redirected to a summary page. That sequence is setup with the following code:
Saving the Information to CouchDB:
The final piece of code that I will show from this example is the server side method that is called from the client to save the information to CouchDB. In the RegistrationSequence function, we saw a call to a function named SaveRegistrationToCouch. This function looks like this:
[<Rpc>] let SaveRegistrationToCouch (registrationInformation:RegistrationInformation) = JsonConvert.SerializeObject registrationInformation |> FSharpCouch.CreateDocument couchDBUrl |> ignoreAs you can see, all it takes for us to expose a server-side method is to add the Rpc attribute to that function.
Viewing the Result:
The result is a nice little registration sequence that contains client-side validation. An example of the initial form with all completed information is below:
This example shows how easy it is to throw together a simple registration form with WebSharper Platform 2010. We've only touched the surface on the capabilities of this platform. You can find out more by visiting the Intellifactory web site. You can find the full solution at http://github.com/dmohl/WebSharper-Example.