[Erlangweb-users] "checked" radio buttons by default on forms
Brought to you by:
etcerlangweb,
paulgray
|
From: <pr...@do...> - 2008-11-16 22:10:43
|
I've been working on a form that contains radio buttons. It's more or less based upon the example application at: http://www.erlang-web.org/code2.htm It doesn't matter (I'm learning much more about the inner working than I otherwise would have if the example did work) but as supplied the example appears to be written for an entirely different version of erlangweb. I have radio buttons for sex. It makes no sense to me to not have one set by default when adding a person to the database, so I wanted one checked rather than having someone miss the setting and generating an error on submission. wpart_enum indeed has code in it to set a "checked" attribute in the radio button HTML tag. I don't however understand the intent or understand how to use it. The "Default" setting from the code is ultimately coming from wpart:fget("__edit") and unless there's a trick involved that I've missed, is set nowhere in the codebase. I'm guessing this is either unfinished code or code that hasn't been removed or I'm confused. It makes sense to me that any {optional, <value>} tuple set on the field type should probably be the value that's checked by default. The following patch does this. It works but I don't know if it's the correct thing to do. It calls the original code and if that fails to find a value it looks up the 'optional' value and uses that for the default instead. --------------------------------------------------------------------------- diff --git a/lib/wparts-1.1/src/wpart_enum.erl b/lib/wparts-1.1/src/wpart_enum.erl index 076dcdf..c6452e5 100644 --- a/lib/wparts-1.1/src/wpart_enum.erl +++ b/lib/wparts-1.1/src/wpart_enum.erl @@ -40,7 +40,13 @@ build_html_tag(Name, Prefix, Params, Default) -> false -> []; {value, {choices, List}} -> List end, - D = wpart_derived:find(N, Default), + D = case wpart_derived:find(N, Default) of + "" -> case lists:keysearch(optional, 1, Params) of + false -> ""; + {value, {optional, D1}} -> D1 + end; + D1 -> D1 + end, wpart_derived:surround_with_table(N, get_html_tag(N, Choices, D), Description). --------------------------------------------------------------------------- - paul |