遇到 Nitrogen 表單驗證的問題

今天在做 Nitrogen 的前端,覺得有點奇怪,寫一些驗證像 #validate { text= “請輸入合適的電子郵件帳號" } 出去,在產生網頁時,都跑出一則內容甚多的錯誤訊息,如下所示。

=ERROR REPORT==== 9-Jun-2015::12:29:57 ===
Ranch listener http had connection process started with cowboy_protocol:start_link/4 at <0.4377.0> exit with reason: {[{reason,badarg},{mfa,{cowboy_simple_bridge_anchor,websocket_handle,3}},{stacktrace,[{erlang,iolist_to_binary,[[<<“nitrogen_event:">>,[ …… (以下好幾十行內容)

後來去看到 cowboy_simple_bridge_anchor.erl 裡面是這樣寫的:

massage_reply({reply, {Type, Data}, NewState}, Req, WSState)
  when Type == binary orelse Type = text ->
    {reply, {Type, iolist_to_binary(Data)}, Req,
     WSState#ws_state{state=NewState}};

將 erlang:iolist_to_binary/1 改成 unicode:characters_to_binary/1 ,如下,即可。

massage_reply({reply, {Type, Data}, NewState}, Req, WSState)
  when Type == binary orelse Type = text ->
    {reply, {Type, unicode:characters_to_binary(Data)}, Req,
     WSState#ws_state{state=NewState}};

另外是動態抽換表單的時候,使用 wf:wire/3 都遇到似乎載入失敗的情況,而改用 wf:defer/3 或 wf:eager/3 ,就可以。

wire() ->
    wf:defer(signup_submit, signup_name,
      #validate { validators=[
	#is_required { text="請輸入您的姓名" },
	#min_length { text="請不要輸入您的姓名少於2字", length=2 },
	#max_length { text="請不要輸入您的姓名超過7字", length=7 }
    ]}),
    wf:defer(signup_submit, signup_e_mail,
      #validate { validators=[
	#is_email { text="請輸入合適的電子郵件帳號" }
    ]}),
    wf:defer(signup_facebook,
             #event { type= click, postback= signup_facebook }),
    wf:defer(signup_submit,
             #event { type= click, postback= signup_submit }),
    wf:defer(signup_login,
             #event { type= click, postback= signup_login }).

form() ->
    wire(),
    [ #panel { id=error }, ......
廣告

About 黃耀賢 (Yau-Hsien Huang)

熱愛 Erlang ,並且有相關工作經驗。喜歡程式語言。喜歡邏輯。目前用 Python 工作。
本篇發表於 Erlang, Web Programming。將永久鏈結加入書籤。