1

I have this checkbox:

<div>
    <input required type="checkbox" id="effectiveness" name="effectiveness">
    <label for="effectiveness">Wirksamkeit</label>
</div>

And this button to submit:

<input type="submit" id="commitChanges" name="commitChanges" value="Freigeben" tal:condition="python: not training['released'] "/>

All inside a form. If the checkbox has an invalid value, it is red from the opening of the page on and not only after pressing the button. Does anyone know how to change that?

Edit (extra info): I don't want the checkbox to be red. I want it to bei normal until i press the button "Wirksamkeit". Than it should turn red, if the value of the checkbox is invalid

Edit:

Here is the full code:

<tr tal:define="effectiveness view/get_effectiveness">
    <td>
        <select name="effectiveness_helper" id="effectiveness_helper" style="display: none;">
            <option value="default">default</option>
            <option value="yes">yes</option>
            <option value="no">no</option>
        </select>
        <div>
          <input required type="checkbox" id="effectiveness" name="effectiveness">
          <label for="effectiveness">Wirksamkeit</label>
        </div>
        <script tal:condition="python: effectiveness['effectiveness'] == 2">
            $("#effectiveness").prop("indeterminate", true).prop("checked", false)
        </script>
        <script tal:condition="python: effectiveness['effectiveness'] == 1">
            $("#effectiveness").prop("checked", true)
        </script>
        <script tal:condition="python: effectiveness['effectiveness'] == 0">
            $("#effectiveness").removeProp('required').prop("checked", false)
        </script>
        <script tal:condition="python: not training['effectiveness_verification']">
            $("#effectiveness").removeProp('required');
        </script>
        <script tal:condition="python: training['released']">
            $("#effectiveness").removeProp('required').prop("disabled", "disabled");
        </script>
        <script>
            $("#effectiveness").change(function() {
                if (!$(this).is(":checked")) {
                    $(this).removeProp('required');
                }
            });
            let checkbox = document.getElementById("effectiveness");
            let dropdown = document.getElementById("effectiveness_helper");

            checkbox.addEventListener("change", (event) => {
                let checked = event.target.checked;
                dropdown.value = checked ? "yes" : "no";
            });
        </script>
    </td>
</tr>

And here is an drop-down menu where it works like i want it to. The drop-down menu gets red only after I clicked the freigeben button.

<tr tal:define="overall view/get_training">
    <td tal:content="overall/rating" tal:condition="python: training['released']"></td>
    <td tal:condition="python: not training['released']" id="rating" >
        <select required size="1" id="rating" name="rating">
            <option value=="">Bitte Auswählen</option>
            <option tal:define="rating overall/rating;" tal:repeat="rate python:view.get_all_training_evaluations(rating)"
                    tal:content="rate/text" tal:attributes="selected python:rate['selected']">
            </option>
        </select>
    </td>
</tr>

The code might look a little weird, because it is a 3-state checkbox and the third state (default) is the only invalid state.

I tried it with an normal 2StateCheckbox for testing and it worked like i would expect. I think the problem is, that i'm setting the property "checked" false. (I think so because, if i'm clicking on the normal Checkbox and setting the value invalid (unchecked) and than setting the focus to another element it also turns red). But I still have no idea how to fix this problem. I already tried to set the focus on another element. I also set the property with jQuery after setting the checked property to false (in other words first $("#effectiveness").prop("checked", false); and second $("#effectiveness").prop('required', true); without setting the required property inside the html tag). But nothing of that worked.

Simon Rechermann
  • 467
  • 3
  • 18
  • 3
    Have you tried any CSS? – rwpk9 Jul 14 '20 at 11:51
  • Can you please describe in more detail what you mean? Sorry i'm very new to all the web stuff. I think the validation including the red color of the checkbox, if the value is invalid, is done by the browser. When i click on the button, the checkbox is still red and additionally an error message, which tells you that the value of the checkbox is invalid, occurs. Thats the behavior i want but the checkbox should only be red after submitting (clicking the button). – Simon Rechermann Jul 14 '20 at 12:10
  • I described it on a answer. – rwpk9 Jul 14 '20 at 12:49
  • You are using a submit button. So I assume we are talking about two distinct page loads here? Then you should be able to render attribute `required` conditionally, i.e. only in the page load that results from a submit (POST). Are you using PHPTAL? It supports [optional attributes](https://phptal.org/manual/en/split/tal-attributes.html). – Ruud Helderman Jul 14 '20 at 12:52
  • I'm using plone, which is using PHPTAL files i think. It's only one page. Yes I already have some conditions but this does not solve my problem so far. I will put the code in my post maybe that can help to better understand my problem. – Simon Rechermann Jul 14 '20 at 13:55
  • Please put your code in the _question_, not in an answer. That's not what answers are for. – Ruud Helderman Jul 14 '20 at 14:02
  • You are right. I edited my question. – Simon Rechermann Jul 14 '20 at 14:31
  • Plone is using the Chameleon template engin which is based on the former Zope Page Templates, not PHPTAL. https://chameleon.readthedocs.io – MrTango Jul 15 '20 at 10:17
  • You should ask Plone questions in our forum, https://community.plone.org where more people will see them and be able to help you – T. Kim Nguyen Jul 16 '20 at 02:44

2 Answers2

2

It's impossible to style a checkbox directly, but you can apply the styling to the label. Below the label is red when the required checkbox is not checked. Styling is not applied when the checkbox is not mandatory.

input:required:not(:checked) + label {
  background: red;
}
<div>
  <input required type="checkbox" id="required">
  <label for="required">Required</label>
  <input type="checkbox" id="notrequired">
  <label for="notrequired">Not required</label>
</div>
Gerard
  • 15,418
  • 5
  • 30
  • 52
1

Check this, you can't apply styles to a checkbox, but you can substitute them by a new full CSS item.

You should change your html to this:

<label class="container">Wirksamkeit
  <input type="checkbox" id="effectiveness" name="effectiveness">
  <span class="checkbox"></span>
</label>

And add some CSS, I already did it for you check it here.

Basically you will hide the original checkbox and create a totally new one on which you can apply any style you want.

EDIT:

I don't want the checkbox to be red. I want it to bei normal until i press the button "Wirksamkeit". Than it should turn red, if the value of the checkbox is invalid – Simon Rechermann

New code, this change on submit, and prevent form to submit if checkbox is not checked.

rwpk9
  • 304
  • 1
  • 15
  • I don't want the checkbox to be red. I want it to bei normal until i press the button "Wirksamkeit". Than it should turn red, if the value of the checkbox is invalid – Simon Rechermann Jul 14 '20 at 13:54
  • Ok then let me write the code again ... try to explain it better next time because I can see the other guy who answered didn't understand what you were asking either – rwpk9 Jul 14 '20 at 20:48
  • Thanks, that would be nice. Yes that's right, he also didn't get it. But now you understand my problem? – Simon Rechermann Jul 15 '20 at 06:13
  • @SimonRechermann check the new edit, hope its usefull, you can play with the CSS if you want a different checkbox. – rwpk9 Jul 15 '20 at 07:29
  • Thank you very much for your efforts. I finally solved the problem. I just had to remove the .prop("checked", false) from the $("#effectiveness").prop("indeterminate", true).prop("checked", false) statement. It was't necessary to set the "default" state unchecked to make it invalid. – Simon Rechermann Jul 15 '20 at 09:03