PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/validators/validator_base_confirmation.erl

http://github.com/zotonic/zotonic
Erlang | 37 lines | 14 code | 5 blank | 18 comment | 1 complexity | 918605f5153e46d44484c27f46c1448e MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%% @doc Validator for checking if the input is the same as another input.
 4
 5%% Copyright 2009 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_base_confirmation).
20-include("zotonic.hrl").
21-export([render_validator/5, validate/5]).
22
23render_validator(confirmation, TriggerId, _TargetId, Args, Context)  ->
24    MatchField = proplists:get_value(match, Args),
25	JsObject   = z_utils:js_object(z_validation:rename_args(Args)), 
26	Script     = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"confirmation\", ">>, JsObject, <<");\n">>],
27	{[MatchField], Script, Context}.
28
29
30%% @spec validate(Type, TriggerId, Values, Args, Context) -> {ok,AcceptedValue} | {error,Id,Error}
31%%          Error = invalid | novalue | {script, Script}
32validate(confirmation, Id, Value, [MatchField], Context) ->
33    MatchValue = z_validation:get_q(MatchField, Context),
34    if 
35        MatchValue =:= Value -> {{ok, Value}, Context};
36        true -> {{error, Id, invalid}, Context}
37    end.