Thread: [Erlangweb-users] date type with optional date
Brought to you by:
etcerlangweb,
paulgray
|
From: Anders K. <and...@er...> - 2010-03-31 10:41:38
|
Hi, I'm trying to use the date type(http://wiki.erlang-web.org/Wparts/BasicTypes/date) with an optional value for one of my input fields, but if I leave the field that has an optional value empty my validation fails. As long as I put a date in all fields the validation works! from my tracing: (<0.3201.0>) call admin_utils:set_active(not_used,user_admin,admin_reports,[[]]) (<0.3201.0>) returned from admin_utils:set_active/4 -> {proceed,[[]]} (<0.3201.0>) call admin_utils:validate({admin_report,create},user_admin,admin_reports,[[]]) (<0.3201.0>) call wtype_date:validate({[{description,"End date (YYYYMMDD)"},{format,"YYYYMMDD"}],"20100330"}) (<0.3201.0>) call wtype_date:convert_input("YYYYMMDD","20100330",[]) (<0.3201.0>) call wtype_date:convert_input("MMDD","0330",[{year,2010}]) (<0.3201.0>) call wtype_date:convert_input("DD","30",[{month,3},{year,2010}]) (<0.3201.0>) call wtype_date:convert_input([],[],[{day,30},{month,3},{year,2010}]) (<0.3201.0>) returned from wtype_date:convert_input/3 -> {2010,3,30} (<0.3201.0>) returned from wtype_date:convert_input/3 -> {2010,3,30} (<0.3201.0>) returned from wtype_date:convert_input/3 -> {2010,3,30} (<0.3201.0>) returned from wtype_date:convert_input/3 -> {2010,3,30} (<0.3201.0>) call wtype_date:check_min([{description,"End date (YYYYMMDD)"},{format,"YYYYMMDD"}],{2010,3,30}) (<0.3201.0>) returned from wtype_date:check_min/2 -> {ok,{2010,3,30}} (<0.3201.0>) call wtype_date:check_max([{description,"End date (YYYYMMDD)"},{format,"YYYYMMDD"}],{2010,3,30}) (<0.3201.0>) returned from wtype_date:check_max/2 -> {ok,{2010,3,30}} (<0.3201.0>) returned from wtype_date:validate/1 -> {ok,{2010,3,30}} (<0.3201.0>) call wtype_date:validate({[{description,"Start date (YYYYMMDD)"}, {format,"YYYYMMDD"}, {optional,{2010,3,29}}], []}) (<0.3201.0>) call wtype_date:convert_input("YYYYMMDD",[],[]) (<0.3201.0>) returned from wtype_date:convert_input/3 -> {error,bad_format} (<0.3201.0>) returned from wtype_date:validate/1 -> {error, {bad_date_format,[]}} (<0.3201.0>) returned from admin_utils:validate/4 -> {error, {user_admin, validate_error, [admin_reports]}} (<0.3201.0>) call wtype_date:get_date("YYYY-MM-DD","20100330") (<0.3201.0>) returned from wtype_date:get_date/2 -> "20100330" and my admin_report.hrl -record(admin_report, { start_date, start_time, end_date, end_time}). -record(admin_report_types, { start_date = {date, [ {description, "Start date (YYYYMMDD)"} , {format, "YYYYMMDD"} , {optional, {2010,03,29}} ]} , start_time = {time, [ {description, "Start time (HHMMSS)"} , {format, "HHMMSS"} ]} , end_date = {date, [ {description, "End date (YYYYMMDD)"} , {format, "YYYYMMDD"} ]} , end_time = {time, [ {description, "End time (HHMMSS)"} , {format, "HHMMSS"} ]}}). any ideas? Cheers, Anders Karlsson --------------------------------------------------- --------------------------------------------------- WE'VE CHANGED NAMES! Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD. www.erlang-solutions.com |
|
From: Roberto A. <rob...@er...> - 2010-03-31 11:14:52
|
Hi all,
> (<0.3201.0>) call wtype_date:validate({[{description,"Start date (YYYYMMDD)"},
> {format,"YYYYMMDD"},
> {optional,{2010,3,29}}],
> []})
The validate function here is called with [] since the user didn't set
any value in the form.
In wtype_date.erl, the validate function is expecting the atom
"undefined" instead, to trigger the search of the optional value.
validate({Types, undefined}) ->
case wpart_valid:is_private(Types) of
true ->
{ok, undefined};
false ->
case lists:keysearch(optional, 1, Types) of
{value, {optional, Default}} ->
{ok, Default};
_ ->
{error, {empty_input, undefined}}
end
end;
The wype_date.erl should match:
validate({Types, []}) ->
Or am I missing something?
Roberto Aloi
--
University of Kent - Erlang Solutions Ltd.
Twitter: @prof3ta
Blog: http://aloiroberto.wordpress.com
---------------------------------------------------
---------------------------------------------------
WE'VE CHANGED NAMES!
Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.
www.erlang-solutions.com
|
|
From: Michał P. <pau...@gm...> - 2010-04-03 19:10:22
|
Hi,
Wiadomość napisana w dniu 2010-03-31, o godz. 13:14, przez Roberto Aloi:
> Hi all,
>
>> (<0.3201.0>) call wtype_date:validate({[{description,"Start date
>> (YYYYMMDD)"},
>> {format,"YYYYMMDD"},
>> {optional,{2010,3,29}}],
>> []})
>
> The validate function here is called with [] since the user didn't set
> any value in the form.
> In wtype_date.erl, the validate function is expecting the atom
> "undefined" instead, to trigger the search of the optional value.
>
> validate({Types, undefined}) ->
> case wpart_valid:is_private(Types) of
> true ->
> {ok, undefined};
> false ->
> case lists:keysearch(optional, 1, Types) of
> {value, {optional, Default}} ->
> {ok, Default};
> _ ->
> {error, {empty_input, undefined}}
> end
> end;
>
> The wype_date.erl should match:
>
> validate({Types, []}) ->
>
> Or am I missing something?
Well, it depends on what would you like to achieve.
In Erlang Web "optional" attribute is activated only when there is no
corresponding field on the form (HTML), thus no value is passed/sent
to the server. When no value is entered into the form, the field is
set to "" and the first validate function clause does not match.
The more I think about it, the more I am convinced to your way of
thinking (or just to keep both behaviours).
>
> Roberto Aloi
> --
> University of Kent - Erlang Solutions Ltd.
> Twitter: @prof3ta
> Blog: http://aloiroberto.wordpress.com
> ---------------------------------------------------
>
> ---------------------------------------------------
>
> WE'VE CHANGED NAMES!
>
> Since January 1st 2010 Erlang Training and Consulting Ltd. has
> become ERLANG SOLUTIONS LTD.
>
> www.erlang-solutions.com
Cheers,
Michal Ptaszek |