A boolean variable can be set using the keywords true
, false
, unknownboolean
, or a WEM expression that returns a boolean type.
When performing a postback, it is essential to send the text "true", "false", or an empty string "" to update the property to the respective values of true
, false
, and unknownboolean
. This requires careful attention when sending data back.
Let’s examine some examples and analyze them.
All examples feature a writable boolean data model property called BooleanProperty
.
Aside from the poor user experience, this approach does not work because when the portal language is set to English, <?attr @BooleanProperty ?>
will output "yes" when @BooleanProperty
is true
and "no" when it is false
. These outputs are invalid boolean values for a postback. As a workaround, you could use <?js @BooleanProperty ?>
instead, as it will output true
, false
, and unknownboolean
as "true", "false", and "null" respectively. However, "null" is an invalid value, so it will be interpreted as unknownboolean
. But please keep this workaround confidential.
Since we typically do not create a boolean text field, let’s explore more common boolean input fields.
In this example, we register the input @BooleanProperty
and create three radio buttons with the text values "true", "false", and an empty string "" to set the value to unknownboolean
. We check the current state of @BooleanProperty
and add the checked
attribute if it corresponds to the option value. Note that if @BooleanProperty
is unknown, checking whether it is true will always return false
, so we do not need to use IsKnown(@BooleanProperty)
for the "true" and "false" options.
This checkbox will set @BooleanProperty
to either true
or unknownboolean
. This behavior is due to how the browser sends post data. If the checkbox is not checked, the browser will ignore it, resulting in the property assigned to this checkbox being set to unknownboolean
. This may not always be the desired outcome, so let’s look at an improved version to address this issue.
In this example, we introduce a hidden input field that will be used to send the data back, along with an intermediate checkbox to read the current checked state in the submitscript
. Depending on whether the checkbox is checked, we set the hidden input field to "true" or "false".
Regardless of the type of widget you are building, even a simple boolean can become complicated if not carefully considered.