2

I have the following code inside a form tag, I would like to have some error text appear underneath the checkboxes however at the moment I am unable to get it to work with the current setup I have below. What am I doing wrong here?

ticketing/competition_checkboxes.html

<div ng-repeat="compCbx in competitionCheckboxes track by $index">
    <input ng-attr-id="cc_comp_{{ $index }}" ng-model="compCbx.isChecked" name="cc_comp" ng-attr-label="cc_comp_label_{{ $index }}"
           type="checkbox" ng-required="compCbx.checkRequired" ng-checked="compCbx.isChecked" form-blur />
    <label ng-attr-id="cc_comp_label_{{ $index }}" ng-attr-for="cc_comp_{{ $index }}" class="fake-label">
        <i ng-attr-id="cc_comp_cbx_{{ $index }}"></i> <p ng-bind-html="compCbx.text | to_trusted"></p>
        <span class="error-msg error-tnc" ng-show="{{formName}}.cc_comp.$touched && {{formName}}.cc_comp.$error.required">
            You must agree to enter the competition
        </span>
    </label>
</div>

The current output on the page is

The form before submission

The form after submission

As you can see the Terms and Conditions checkbox validates correctly, however that is because it is not in an ng-repeat

The Terms and conditions has this code

<input id="cc_tnc" ng-model="cc_tnc" label="cc_tnc_label" name="cc_tnc" type="checkbox" ng-required="cc_tnc != true" form-blur>
        <label id="cc_tnc_label" for="cc_tnc" class="fake-label">
            <i id="cc_tnc_cbx"></i> <p>I have read and agree to <a href="@Model.TermsUrl" target="_blank">terms and conditions</a></p>
            <span class="error-msg error-tnc" ng-show="{{ formName }}.cc_tnc.$touched && {{ formName }}.cc_tnc.$error.required">
                    You must agree to the Terms and Conditions to make a purchase
            </span>
        </label>
<ng-include src="'ticketing/competition_checkboxes.html'"></ng-include>

Form Blur directive

directive('formBlur', function(setErrorState) {

        //set element on dirty
        return {
            restrict: 'A,E',
            require: ['^form'],
            link: function(scope, element, attrs, ctrl) {

                var form = scope[ctrl[0].$name];

                element.bind('blur', function() {

                    scope.$apply(function() {

                        console.log("Error in blur");
                        form[attrs.name].$touched = true;
                        setErrorState(element, form[attrs.name].$valid);

                    });
                });

                scope.$watch(form.$name + '.' + attrs.name + '.$valid', function(validity) {

                    console.log("In watch");
                    console.dir(form[attrs.name]);
                    if (form[attrs.name].$touched)
                        setErrorState(element, validity);

                });
            }
        };
    })
3
  • What is the content of "fake-label" css class? What is the current output; please provide html rendered by your browser. Commented Feb 13, 2017 at 0:26
  • @osmanraifgunes the fake-label class simply contains some formatting for margins etc. Its not effecting the display. I have added more details to the question as requested Commented Feb 13, 2017 at 0:44
  • @osmanraifgunes updated with new details Commented Feb 15, 2017 at 23:57

1 Answer 1

1

Change ng-required="compCbx.checkRequired" to required="compCbx.checkRequired"

Jsfiddle working sample

I don't know why it is working ;)

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't seem to be working in the fiddle? If I set checkRequired to false it still expects the checkbox to be checked... I believe that the actual issue is caused by attempting to validate using ng-show="{{formName}}.cc_comp.$touched && {{formName}}.cc_comp.$error.required" since the name field on the input are not allowed to have configurable names. Using angular subforms seems to get closer to a solution, but its still not right....

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.