Current section
Files
Jump to
Current section
Files
src/validators/validator_base_presence.erl
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2009-2026 Marc Worrell
%% @doc Validator for checking if a input has been filled/checked in.
%% @end
%% Copyright 2009-2026 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(validator_base_presence).
-moduledoc("
Check if an input has been filled in or checked.
For example when a title must be entered:
```django
<input type=\"text\" id=\"title\" name=\"title\" value=\"\" />
{% validate id=\"title\" type={presence} %}
```
Arguments
---------
| Argument | Description | Example |
| ----------------- | ---------------------------------------------------------- | --------------------------------- |
| failure\\\\_message | Message to be shown when field is empty. Defaults to “\\\\*” | `failure_message=\"Please enter.\"` |
See also
[Forms and validation](/id/doc_developerguide_forms_and_validation#guide-validators)").
-include_lib("zotonic_core/include/zotonic.hrl").
-export([render_validator/5, validate/5]).
render_validator(presence, TriggerId, _TargetId, Args, _Context) ->
JsObject = z_utils:js_object(z_validation:rename_args(Args)),
Script = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"presence\", ">>, JsObject, <<");\n">>],
{[], Script}.
-spec validate(term(), term(), term(), list(), z:context()) ->
{{ok, term()}, z:context()} | {{error, term(), term()}, z:context()}.
%% Error = invalid | novalue | {script, Script}
validate(presence, Id, undefined, _Args, Context) ->
{{error, Id, novalue}, Context};
validate(presence, Id, [], _Args, Context) ->
{{error, Id, novalue}, Context};
validate(presence, _Id, #upload{} = Value, _Args, Context) ->
{{ok, Value}, Context};
validate(presence, Id, Value, _Args, Context) ->
case z_string:trim(Value) of
<<>> -> {{error, Id, invalid}, Context};
_Trimmed -> {{ok, Value}, Context}
end.