/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. %% Copyright 2009 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(validator_base_confirmation).
  18. -include("zotonic.hrl").
  19. -export([render_validator/5, validate/5]).
  20. render_validator(confirmation, TriggerId, _TargetId, Args, Context) ->
  21. MatchField = proplists:get_value(match, Args),
  22. JsObject = z_utils:js_object(z_validation:rename_args(Args)),
  23. Script = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"confirmation\", ">>, JsObject, <<");\n">>],
  24. {[MatchField], Script, Context}.
  25. %% @spec validate(Type, TriggerId, Values, Args, Context) -> {ok,AcceptedValue} | {error,Id,Error}
  26. %% Error = invalid | novalue | {script, Script}
  27. validate(confirmation, Id, Value, [MatchField], Context) ->
  28. MatchValue = z_validation:get_q(MatchField, Context),
  29. if
  30. MatchValue =:= Value -> {{ok, Value}, Context};
  31. true -> {{error, Id, invalid}, Context}
  32. end.