I have a form that needs to be validated.

The form contains many parts, and some of them are disabled by default. The value in each field is correct, but it's against my validation directives. For example, when it's disable it should contain 0, but when it's editable it should contains something else. Anyway I attach a disable directive to them, and put them down.

When i submit my form (using angular scope function), if ($scope.sarfaslForm.$invalid) --> returns true. It says I have two invalid fields when I check the $scope.sarfaslForm.$error list.

following this blog i implement my validations: http://blog.yodersolutions.com/bootstrap-form-validation-done-right-in-angularjs/

And following this thread, I create a directived to ignore some of my disabled control:

I made a minor change to this directive:

.directive('hsDisableValidation', function() {
    return {
        require: '^form',
        restrict: 'A',
        link: function(scope, element, attrs,form) {
            var control;

            scope.$watch(function() {
                return scope.$eval(attrs.hsDisableValidation);
            }, function(value) {
                if (!control) {
                    control = form[element[0].name];
                    //form[element.find("input").attr("name")];
                }
                if (value === false) {
                    form.$addControl(control);
                    angular.forEach(control.$error, function(validity, validationToken) {
                        form.$setValidity(validationToken, !validity, control);
                    });
                } else {
                    form.$removeControl(control);
                    //In Here: I tried to $setValidity of controls to true, Remove Error List, and Remove Validator Function, but these things didn't worked out
                }
            });
        }
    }
})

Here the validation always failed for me: PS: Since i use it on 'text' typed field, i have no min/max value, only min/max for length. I'm sure it's not the issue, but I'm including this code to make sure.

.directive('hsMinValue', function() {
        return {
            require: 'ngModel',
            link: function (scope, elem, attr, ngModel) {

                function isLesser(value) {
                    var minVal = parseInt(attr.hsMinValue);
                    return parseInt(value) < minVal;
                }

                function validate(value) {
                    var valid = !isLesser(value);
                    ngModel.$setValidity('minValue', valid);
                    return valid ? value : undefined;
                }

                ngModel.$parsers.unshift(function (value) {
                    ngModel.$setValidity('minValue', !isLesser(value));
                    return value;
                });

                ngModel.$formatters.unshift(function (value) {
                    ngModel.$setValidity('minValue', !isLesser(value));
                    return value;
                });
            }
        }
    });

and then I save:

saveSarfasl: function () {
                $scope.$broadcast('show-errors-check-validity');
                if ($scope.sarfaslForm.$invalid) { --> True :|
                    return;
                }
                //Stuffs
}

Edit: In Request of James, I Put The HTML And A View Of My Page In Here.

View of the HTML Form

<div class="clearfix">
    <form name="sarfaslForm" novalidate>
        <table class="table-condensed">
            <tbody>
                <tr>
                    <td>
                        کد سرفصل
                    </td>
                    <td>
                        <table class="table-condensed">
                            <tbody>
                                <tr>
                                    <td data-ng-if="View.FinYear.LenK != 0">
                                        کل
                                    </td>
                                    <td data-ng-if="View.FinYear.LenM != 0">
                                        معین
                                    </td>
                                    <td data-ng-if="View.FinYear.LenT1 != 0">
                                        تفضیل یک
                                    </td>
                                    <td data-ng-if="View.FinYear.LenT2 != 0">
                                        تفضیل دو
                                    </td>
                                    <td data-ng-if="View.FinYear.LenJ != 0">
                                        جزء
                                    </td>
                                </tr>
                                <tr>
                                    <td data-ng-if="View.FinYear.LenK != 0">
                                        <div class="form-group" hs-show-errors hs-show-success>
                                            <input name="CodKol" type="text" hs-restrict-pattern="[^\d]*" maxlength="{{View.FinYear.LenK}}"
                                                   class="form-control input-sm"
                                                   data-ng-model="View.Kol" data-ng-disabled="View.Level!==1"
                                                   data-ng-blur="Events.codeChanged('k')"
                                                   data-ng-change="Events.setSarfaslCod()"
                                                   hs-disable-validation="View.Level!==1"
                                                   data-ng-required="true"
                                                   hs-min-value="1" />
                                            <p class="help-block" ng-if="sarfaslForm.CodKol.$error.required">
                                                کد کل الظامی می باشد
                                            </p>
                                            <p class="help-block" ng-if="sarfaslForm.CodKol.$error.minValue">
                                                کد کل نمی تواند صفر باشد
                                            </p>
                                        </div>
                                    </td>
                                    <td data-ng-if="View.FinYear.LenM != 0">
                                        <div class="form-group" hs-show-errors hs-show-success>
                                            <input name="CodMoin" type="text" hs-restrict-pattern="[^\d]*" maxlength="{{View.FinYear.LenM}}"
                                                   class="form-control input-sm"
                                                   data-ng-model="View.Moin" data-ng-disabled="View.Level!==2"
                                                   data-ng-blur="Events.codeChanged('m')"
                                                   data-ng-change="Events.setSarfaslCod()"
                                                   hs-disable-validation="View.Level!==2"
                                                   data-ng-required
                                                   hs-min-value="1" />
                                            <p class="help-block" ng-if="sarfaslForm.CodMoin.$error.required">
                                                کد معین الظامی می باشد
                                            </p>
                                            <p class="help-block" ng-if="sarfaslForm.CodMoin.$error.minValue">
                                                کد معین نمی تواند صفر باشد
                                            </p>
                                        </div>
                                    </td>
                                    <td data-ng-if="View.FinYear.LenT1 != 0">
                                        <div class="form-group" hs-show-errors hs-show-success>
                                            <input name="CodTafzil1" type="text" hs-restrict-pattern="[^\d]*" maxlength="{{View.FinYear.LenT1}}"
                                                   class="form-control input-sm"
                                                   data-ng-model="View.Tafzil1" data-ng-disabled="View.Level!==3"
                                                   data-ng-blur="Events.codeChanged('t1')"
                                                   data-ng-change="Events.setSarfaslCod()"
                                                   hs-disable-validation="View.Level!==3"
                                                   data-ng-required
                                                   hs-min-value="1" />
                                            <p class="help-block" ng-if="sarfaslForm.CodTafzil1.$error.required">
                                                کد تفظیل یک الظامی می باشد
                                            </p>
                                            <p class="help-block" ng-if="sarfaslForm.CodTafzil1.$error.minValue">
                                                کد تفظیل یک نمی تواند صفر باشد
                                            </p>
                                        </div>
                                    </td>
                                    <td data-ng-if="View.FinYear.LenT2 != 0">
                                        <div class="form-group" hs-show-errors hs-show-success>
                                            <input name="CodTafzil2" type="text" hs-restrict-pattern="[^\d]*" maxlength="{{View.FinYear.LenT2}}"
                                                   class="form-control input-sm"
                                                   data-ng-model="View.Tafzil2" data-ng-disabled="View.Level!==4"
                                                   data-ng-blur="Events.codeChanged('t2')"
                                                   data-ng-change="Events.setSarfaslCod()"
                                                   hs-disable-validation="View.Level!==4"
                                                   data-ng-required
                                                   hs-min-value="1" />
                                            <p class="help-block" ng-if="sarfaslForm.CodTafzil2.$error.required">
                                                کد تفظیل دو الظامی می باشد
                                            </p>
                                            <p class="help-block" ng-if="sarfaslForm.CodTafzil2.$error.minValue">
                                                کد تفظیل دو نمی تواند صفر باشد
                                            </p>
                                        </div>
                                    </td>
                                    <td data-ng-if="View.FinYear.LenJ != 0">
                                        <div class="form-group" hs-show-errors hs-show-success>
                                            <input name="CodJoz" type="text" hs-restrict-pattern="[^\d]*" maxlength="{{View.FinYear.LenJ}}"
                                                   class="form-control input-sm"
                                                   data-ng-model="View.Joz" data-ng-disabled="View.Level!==5"
                                                   data-ng-blur="Events.codeChanged('j')"
                                                   data-ng-change="Events.setSarfaslCod()"
                                                   hs-disable-validation="View.Level!==5"
                                                   data-ng-required
                                                   hs-min-value="1" />
                                            <p class="help-block" ng-if="sarfaslForm.CodJoz.$error.required">
                                                کد جزء الظامی می باشد
                                            </p>
                                            <p class="help-block" ng-if="sarfaslForm.CodJoz.$error.minValue">
                                                کد جزء نمی تواند صفر باشد
                                            </p>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>
                        عنوان سرفصل
                    </td>
                    <td ng-class="{'has-error':sarfaslForm.HsbNam.$invalid && sarfaslForm.HsbNam.$dirty}">
                        <div class="form-group" hs-show-errors hs-show-success>
                            <input name="HsbNam" type="text" data-ng-model="View.Sarfasl.HsbNam"
                                   class="form-control input-sm"
                                   data-ng-required="true" />
                            <p class="help-block" ng-if="sarfaslForm.HsbNam.$error.required">
                                نام سرفصل الظامی می باشد
                            </p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.HsbKind" name="HsbKind" data-ng-value="'00'"
                                        data-ng-required="true" />
                                زیر سطح دارد
                            </label>
                        </div>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.HsbKind" name="HsbKind" data-ng-value="'11'"
                                       data-ng-required="true" />
                                سطح آخر است
                            </label>
                            <p class="help-block" ng-if="sarfaslForm.HsbKind.$error.required">
                                انتخاب یکی از حالات الظامی می باشد
                            </p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.Permanent" name="Permanent" data-ng-value="'1'"
                                       data-ng-required="true" />
                                حساب دائم
                            </label>
                        </div>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.Permanent" name="Permanent" data-ng-value="'0'"
                                       data-ng-required="true" />
                                حساب موقت
                            </label>
                            <p class="help-block" ng-if="sarfaslForm.Permanent.$error.required">
                                انتخاب یکی از حالات الظامی می باشد
                            </p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.AccessFlag" name="AccessFlag" data-ng-value="0"
                                       data-ng-required="true" />
                                حساب برای همه در دسترس باشد
                            </label>
                        </div>
                        <div class="radio" hs-show-errors>
                            <label class="control-label">
                                <input type="radio" data-ng-model="View.Sarfasl.AccessFlag" name="AccessFlag" data-ng-value="1"
                                       data-ng-required="true" />
                                حساب فقط برای کاربران زیر در دسترس باید
                            </label>
                            <p class="help-block" ng-if="sarfaslForm.AccessFlag.$error.required">
                                انتخاب یکی از حالات الظامی می باشد
                            </p>
                        </div>
                    </td>
                </tr>
                <tr data-ng-if="View.Sarfasl.AccessFlag==1">
                    <td>

                    </td>
                    <td>
                        <table class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>
                                    &nbsp;
                                </th>
                                <th>
                                    کاربران
                                </th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr data-ng-repeat="user in View.UserList">
                                <td>
                                    <div class="checkbox">
                                        <label class="control-label">
                                            <input type="checkbox" data-ng-model="user.Checked" data-ng-click="Events.userChecking(user)" />
                                        </label>
                                    </div>
                                </td>
                                <td>
                                    {{user.UserID}}
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <!-- Hsb Types -->
                        <div class="checkbox">
                            <label class="control-label">
                                <input type="checkbox" data-ng-model="View.CheckBoxAllowRegisterLiability"
                                       data-ng-click="Events.hsbTypeChecking(View.HsbTypes.AllowRegisterLiability, View.CheckBoxAllowRegisterLiability)" />
                                اجازه ثبت بدهکاری در اسناد داشته باشد
                            </label>
                        </div>
                        <div class="checkbox">
                            <label class="control-label">
                                <input type="checkbox" data-ng-model="View.CheckBoxAllowRegisterCredits"
                                       data-ng-click="Events.hsbTypeChecking(View.HsbTypes.AllowRegisterCredits, View.CheckBoxAllowRegisterCredits)" />
                                اجازه ثبت بستانکاری در اسناد داشته باشد
                            </label>
                        </div>
                        <div class="checkbox">
                            <label class="control-label">
                                <input type="checkbox" data-ng-model="View.CheckBoxReminderShouldOnlyBeDebtor"
                                       data-ng-click="Events.hsbTypeChecking(View.HsbTypes.ReminderShouldOnlyBeDebtor, View.CheckBoxReminderShouldOnlyBeDebtor)" />
                                مانده حساب فقط باید بدهکار باشد
                            </label>
                        </div>
                        <div class="checkbox">
                            <label class="control-label">
                                <input type="checkbox" data-ng-model="View.CheckBoxReminderShouldOnlyBeCreditor"
                                       data-ng-click="Events.hsbTypeChecking(View.HsbTypes.ReminderShouldOnlyBeCreditor, View.CheckBoxReminderShouldOnlyBeCreditor)" />
                                مانده حساب فقط باید بستانکار باشد
                            </label>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        یادداشت
                    </td>
                    <td>
                        <textarea data-ng-model="View.Sarfasl.YadDasht" cols="200" rows="4" class="form-control input-sm">

                        </textarea>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <button type="button" class="btn btn-sm btn-primary" data-ng-click="Events.saveSarfasl()">
                            ذخیره اطلاعات
                        </button>
                        <a href="/#/Sarfasl" class="btn btn-sm btn-primary">
                            بازگشت
                        </a>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</div>

Edit 2:

When i use this, all controls work well on view each one by it self. But after submit ( which the submit button is normal button, and has no effect, but calling my function) in my function, i see errors in overall result: $scope.sarfaslForm.$invalid ==> true

share|improve this question
    
Can I get a look at your html markup for your form or at least the fields you are having problem with? – James Aug 17 '15 at 15:01
    
i cannot provide you with data at this time, as i'm already home, tommorow i can post exact HTML, but it's a mess... But Telling You, my Html, contains 7 text field, and several radio , checkboxes, ... 6 text fields, and all radios have validations, the 5 of these text field, create a code, which i made them the way my boss systems work, so i couldn't change that old system, and he want it this way, so my hands are tide. so i put 5 field on screen, which depend on other model, i calculate which field should be exist, and then using ng-if , remove others from sceen, thouse who remain should – deadManN Aug 17 '15 at 15:52
    
I guess it would just would be helpful to know which specific fields are failing validation, it is possible to make a custom validation for the fields that are failing to resole the issue but it is really hard for me to tell from your description so far. – James Aug 17 '15 at 15:55
    
be field one by one, each time i come to this form, they together form a code, and each section can be called parent of another one, but as i don't wanna create lot of forms, so stick with my validation scenario,... so first time field one is active, other field filled with zeros are are disable, and some of others, may be invisible..., next time first field filled with data, second is active, and 3rd filled with zeros, if the third one is the last visible field, then on 3rd time we go to the form, first and second field are filled with data, and the 3rd one is activate. – deadManN Aug 17 '15 at 15:57
1  
if you setup a plunkr with your specific situation, you stand a better chance of getting an answer. – sirrocco Aug 23 '15 at 4:08

Alright here is the assumption your code is making. When you are looking through to check for validation you are checking to see if value === false otherwise you are adding it to your form, which isn't the logic you are describing you want.

What you are stating is that no matter the value if the field is disabled and set to 0 I want it removed from form, then if the value is false I want it removed from form else validate the form. Just have a logic check to see if the field is disabled or not and remove it from validation.

 if (value === 0) {
    form.$removeControl(control);
 } else if (value === false) {
    form.$addControl(control);
    angular.forEach(control.$error, function(validity, validationToken) {
    form.$setValidity(validationToken, !validity, control);
                    });
 } else {
     form.$removeControl(control);
 }
share|improve this answer
    
First of all, sorry for trouble i caused due i'm not native English talker. the section consist of 5 parts, depend on conditions some should be shown, and the others shouldn't ... that's when i used ng-if, but it is no problem to me. the problem is on component that should be shown... they again are consist of several items (item that are enable or item that are disable), there i used data-ng-disabled="condition" & hs-disable-validation="condition"... when i use hs-disable-validation it should talk to validation system, and tell it do not observe this element. that's where i'm failed – deadManN Aug 18 '15 at 13:00
    
form.$removeControl(control) run well even on my sample, i mean it goes through all of component remove all controls, then again add controls to enable ones, but once i check for overall validation, it say there are errors, ... One by one they are fine, but overall ... no – deadManN Aug 18 '15 at 13:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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