Advertisement
Guest User

ui.next validation

a guest
Jul 28th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.51 KB | None | 0 0
  1. namespace Validation_Scratchpad
  2.  
  3. open System
  4. open IntelliFactory.WebSharper
  5. open IntelliFactory.WebSharper.UI.Next
  6. open IntelliFactory.WebSharper.UI.Next.Html
  7.  
  8. [<JavaScript>]
  9. module Client =
  10.  
  11.     type EMailAddress = EMail of string
  12.     type TelephoneNumber = TN of string
  13.  
  14.     let showPhone (TN s) = s
  15.     let showEMail (EMail e) = e
  16.  
  17.     type Person =
  18.         {
  19.             PersonName : string
  20.             PersonEMailAddress : EMailAddress
  21.             PersonTelephoneNumber : TelephoneNumber
  22.         }
  23.  
  24.     let mkPerson name num email =
  25.         {
  26.             PersonName = name
  27.             PersonEMailAddress = email
  28.             PersonTelephoneNumber = num
  29.         }
  30.  
  31.     type ErrorMsg = string
  32.     type Result<'a> = | Success of 'a | Failure of ErrorMsg
  33.  
  34.     let txt = Doc.TextNode
  35.     let (==>) attrId attrVal = Attr.Create attrId attrVal
  36.  
  37.     let validateNum vNum =
  38.         View.FromVar vNum
  39.         |> View.Map (fun num ->
  40.             if String.forall Char.IsDigit num && num.Length > 0 then
  41.                 TN num |> Success
  42.             else
  43.                 Failure "Not a valid telephone number"
  44.         )
  45.  
  46.     let validateEmail vEmail =
  47.         View.FromVar vEmail
  48.         |> View.Map (fun (email : string) ->
  49.             if email.Contains("@") then
  50.                 EMail email |> Success
  51.             else
  52.                 Failure "Not a valid email address"
  53.         )
  54.  
  55.     let DisplayFailure = function
  56.         | Success _ -> ""
  57.         | Failure err -> err + ", "
  58.  
  59.     let PersonView nameView telView emailView =
  60.         View.Do {
  61.             let! name = nameView
  62.             let! tel = telView
  63.             let! email = emailView
  64.             match (tel, email) with
  65.                 | (Success t, Success e) ->
  66.                     return mkPerson name t e |> Success
  67.                 | _ ->
  68.                     return
  69.                         DisplayFailure tel + DisplayFailure email
  70.                         |> Failure
  71.         }
  72.  
  73.     let showPerson p =
  74.         Div0 [
  75.             P0 [txt <| "Name: " + p.PersonName]
  76.             P0 [txt <| "Phone Number: " + (showPhone p.PersonTelephoneNumber)]
  77.             P0 [txt <| "EMail Address: " + (showEMail p.PersonEMailAddress)]
  78.         ]
  79.  
  80.     let ShowResult personView =
  81.         personView
  82.         |> View.Map (fun personRes ->
  83.             match personRes with
  84.                 | Success p -> showPerson p
  85.                 | Failure errs -> txt errs
  86.         )
  87.  
  88.     let myForm =
  89.         let rvName = Var.Create ""
  90.         let rvTel = Var.Create ""
  91.         let rvEmail = Var.Create ""
  92.  
  93.         Div0 [
  94.             Div0 [
  95.                 Div0 [
  96.                     Html.Label ["for" ==> "nameBox"] [txt "Name:"]
  97.                     Doc.Input ["id" ==> "nameBox"] rvName
  98.                 ]
  99.  
  100.                 Div0 [
  101.                     Html.Label ["for" ==> "telBox"] [txt "Telephone Number:"]
  102.                     Doc.Input ["id" ==> "telBox"] rvTel
  103.                 ]
  104.  
  105.                 Div0 [
  106.                     Html.Label ["for" ==> "emailBox"] [txt "Email:"]
  107.                     Doc.Input ["id" ==> "emailBox"] rvEmail
  108.                 ]
  109.             ]
  110.  
  111.             Div0 [
  112.                 txt "Result"
  113.                 Elements.Br [][]
  114.                 PersonView (View.FromVar rvName) (validateNum rvTel) (validateEmail rvEmail)
  115.                 |> ShowResult
  116.                 |> Doc.EmbedView
  117.             ]
  118.         ]
  119.  
  120.     let Main =
  121.         JavaScript.Log("Running JavaScript Entry Point..")
  122.         Doc.RunById "my-container" myForm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement