PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_admin_identity/validators/validator_admin_identity_username_unique.erl

https://code.google.com/p/zotonic/
Erlang | 64 lines | 39 code | 5 blank | 20 comment | 0 complexity | becc424079008b9daeb4ea2fd1163b58 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2010 Marc Worrell
 3%% @doc Check if an entered username is unique
 4
 5%% Copyright 2010 Marc Worrell
 6%%
 7%% Licensed under the Apache License, Version 2.0 (the "License");
 8%% you may not use this file except in compliance with the License.
 9%% You may obtain a copy of the License at
10%% 
11%%     http://www.apache.org/licenses/LICENSE-2.0
12%% 
13%% Unless required by applicable law or agreed to in writing, software
14%% distributed under the License is distributed on an "AS IS" BASIS,
15%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16%% See the License for the specific language governing permissions and
17%% limitations under the License.
18
19-module(validator_admin_identity_username_unique).
20-include("zotonic.hrl").
21-export([
22    render_validator/5,
23    validate/5,
24    event/2
25]).
26
27render_validator(username_unique, TriggerId, TargetId, Args, Context)  ->
28    {_PostbackJS, PostbackInfo} = z_render:make_postback({validate, Args}, 'postback', TriggerId, TargetId, ?MODULE, Context),
29    JsObject = z_utils:js_object(z_validation:rename_args([{z_postback, PostbackInfo}|Args])),
30    Script = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"postback\", ">>, JsObject, <<");\n">>],
31    {Args, Script, Context}.
32
33%% @spec validate(Type, TriggerId, Value, Args, Context) -> {{ok,AcceptedValue}, NewContext} | {{error,Id,Error}, NewContext}
34%%          Error = invalid | novalue | {script, Script} | novalidator | string()
35validate(username_unique, Id, Value, Args, Context) ->
36    UserId = z_convert:to_integer(proplists:get_value(id, Args)),
37    Username = z_string:trim(Value),
38    case Username of
39        [] ->
40            {{ok, []}, Context};
41        _ ->
42            case m_identity:lookup_by_username(Username, Context) of
43                undefined ->
44                    {{ok, Username}, Context};
45                Identity -> 
46                    case proplists:get_value(rsc_id, Identity) of
47                        UserId -> {{ok, Username}, Context};
48                        _Other -> {{error, Id, invalid}, Context}
49                    end
50            end
51    end.
52
53%% @spec event(Event, Context) -> Context
54%% @doc Handle the validation during form entry.
55event({postback, {validate, Args}, TriggerId, _TargetId}, Context) ->
56    Value = z_context:get_q(triggervalue, Context),
57    {IsValid, ContextValidated} = case validate(username_unique, TriggerId, Value, Args, Context) of
58        {{ok, _}, ContextOk} -> 
59            {"true", z_render:wire({fade_out, [{target, TriggerId ++ "_username_unique_error"}]}, ContextOk)};
60        {{error, Id, _} = Error, ContextScript} -> 
61            {"false", z_render:wire({fade_in, [{target, TriggerId ++ "_username_unique_error"}]},
62                                    z_validation:report_errors([{Id,Error}], ContextScript))}
63    end,
64    z_script:add_script(["z_async_validation_result('",TriggerId,"', ",IsValid,", '",z_utils:js_escape(Value),"');"], ContextValidated).