Question regarding checkboxes and ColdBox. I am tr...
# box-products
o
Question regarding checkboxes and ColdBox. I am trying to use a checkbox to allow the user to choose if they wish to receive notifications. There is weird behavior going on at least for me, If the checkbox is ticketed and i use
writeOutput()
to get it's output it displays as on. However when I it isn't ticketed it simply doesn't exist. I thought it would output either
true
or
false
at least logically. Am I missing something or am I going to have to do a bit more work to get it to do what I want?
m
that's a normal form field behavior for checkboxes. there is nothing passed along for checkbox or a select multi that haven't been picked. The common pattern is generally to param those in the processing code to the unchecked state
o
I don't understand what you mean. So far I tried
isNull()
, and
isEmpty()
to no success. Trying
structKeyExists()
now.
m
param name="" default=""; then the field will exist and its simpler to work with. structKeyExists would work fine too
also, fyi, it's not a coldbox thing, you can demo it in any cfml page
a
(or any HTML page, as it's just an HTML thing). @Ookma-Kyi if yer working with HTML, you need to understand how it works. There is plenty of online material out there explaining the various elements work. This is a relevant description of how a checkbox's unchecked state is sent to the server (ie: it isn't): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#sect2
o
That is actually weird cause I used to do Python and I never had to deal with any of this which is why I thought it was related to the framework. Snip:
Copy code
if request.method == "POST":
        form = UpdateNotificationPreferenceForm(<http://request.POST|request.POST>, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("user_profile"))
And yes it was a Boolean value that field:
Copy code
# Create your models here.
class Profile(models.Model):
    """Model representing a single user profile."""

    """The users notification preference, if true will send notifications to the user."""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    notification_preference = models.BooleanField(
        default=False,
        help_text="If ticked will send you notifications."
    )
a
checkboxes are HTML. Python has got nothing to do with. The behaviour of HTML checkboxes do not change based on what server-side language you are using. You really need to take the time to understand the tools you are using.
o
Just saying that when I handled this same situation in Python + Django this particular situation where it didn't exist if it wasn't checked never arose. This is new to me.
a
Probably because something in Django had been configured to expect a post field of that name, and defaulted it to [whatevs] when it wasn't received. Shrug. I can't possibly say. This does not detract from the fact you are being confused by standard HTML behaviour, and this is less than ideal for a web developer. Irrespective of what other things you have used in the past. Anyway: only so many ways/times I can say the same thing so I'll give up.
o
Ok I'll take your advice. What tools am I supposed to learn about?
a
HTML
c
Unless I'm mistaken, that Python snippet above was setting a default value in the model:
Copy code
notification_preference = models.BooleanField(
        default=False,
o
That is true. I guess Django was casting it behind the scenes for me.