0

I have a Form that includes two radio inputs both based on this field structure:

<field name="privacy_policy"
  type="radio"
  layout="joomla.form.field.radio.switcher"
  default="0"
  required="true"
  validate="ValidateChecked"
  class="validate-checked"
  label="COM_COMPONENTNAME_FIELD_PRIVACY_STATUS_LABEL"
>
    <option value="0">JNO</option>
    <option value="1">JYES</option>
</field>

And the Rule JS:

document.addEventListener('DOMContentLoaded', function() {
    document.formvalidator.setHandler("checked", function (value, element) {
        console.log(element)
        console.log(value)
        return value == "1";
    });
});

The rule gets called like so on save BUT its called twice (once for the option no and once for the option yes. This means the form does not get submitted. So how can I implement a check to make sure the Radio was switched to yes?

See Console Log: Console Log

1 Answer 1

0

Ok got it now The "problem" is the structure of the radio field, it is based on two inputs. The check runs on each of these input elements. So we need to check the whole status of both fields which means we need also to check if the option with value "0" is NOT selected see updated JS for checked rule:

document.addEventListener('DOMContentLoaded', function() {
    document.formvalidator.setHandler("checked", function (value, element) {
        return value === "1" && element.checked || value === "0" && !element.checked;
    });
});

The Code above runs on EVERY input of the radio field (in this case both - the yes and no option) so we need to check that the option with value "0" is not checked for sure and it has also to work if we check that the option with the value "1" and make sure that this option IS checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.