Trigger validation of all fields in Angular Form submit

后端 未结 13 1340
悲哀的现实
悲哀的现实 2020-12-08 01:45

I\'m using this method: http://plnkr.co/edit/A6gvyoXbBd2kfToPmiiA?p=preview to only validate fields on blur. This works fine, but I would also like to validate them (and thu

相关标签:
13条回答
  • 2020-12-08 02:12

    I done something following to make it work.

    <form name="form" name="plantRegistrationForm">
      <div ng-class="{ 'has-error': (form.$submitted || form.headerName.$touched) && form.headerName.$invalid }">
        <div class="col-md-3">
          <div class="label-color">HEADER NAME 
            <span class="red"><strong>*</strong></span></div>
        </div>
        <div class="col-md-9">
          <input type="text" name="headerName" id="headerName" 
                 ng-model="header.headerName" 
                 maxlength="100" 
                 class="form-control" required>
          <div ng-show="form.$submitted || form.headerName.$touched">
            <span ng-show="form.headerName.$invalid" 
                  class="label-color validation-message">Header Name is required</span>
          </div>
        </div>
      </div>
    
      <button ng-click="addHeader(form, header)" 
              type="button" 
              class="btn btn-default pull-right">Add Header
      </button>
    
    </form>
    

    In your controller you can do;

    addHeader(form, header){
            let self = this;
            form.$submitted = true;
            ... 
        }
    

    You need some css as well;

    .label-color {
                color: $gray-color;
            }
    .has-error {
           .label-color {
                color: rgb(221, 25, 29);
            }
            .select2-choice.ui-select-match.select2-default {
                border-color: #e84e40;
            }
        }
    .validation-message {
           font-size: 0.875em;
        }
        .max-width {
            width: 100%;
            min-width: 100%;
        }
    
    0 讨论(0)
  • 2020-12-08 02:13

    Well, the angular way would be to let it handle validation, - since it does at every model change - and only show the result to the user, when you want.

    In this case you decide when to show the errors, you just have to set a flag: http://plnkr.co/edit/0NNCpQKhbLTYMZaxMQ9l?p=preview

    As far as I know there is a issue filed to angular to let us have more advanced form control. Since it is not solved i would use this instead of reinventing all the existing validation methods.

    edit: But if you insist on your way, here is your modified fiddle with validation before submit. http://plnkr.co/edit/Xfr7X6JXPhY9lFL3hnOw?p=preview The controller broadcast an event when the button is clicked, and the directive does the validation magic.

    0 讨论(0)
  • 2020-12-08 02:18

    What worked for me was using the $setSubmitted function, which first shows up in the angular docs in version 1.3.20.

    In the click event where I wanted to trigger the validation, I did the following:

    vm.triggerSubmit = function() {
        vm.homeForm.$setSubmitted();
        ...
    }
    

    That was all it took for me. According to the docs it "Sets the form to its submitted state." It's mentioned here.

    0 讨论(0)
  • 2020-12-08 02:18

    Based on Thilak's answer I was able to come up with this solution...

    Since my form fields only show validation messages if a field is invalid, and has been touched by the user I was able to use this code triggered by a button to show my invalid fields:

    // Show/trigger any validation errors for this step
    angular.forEach(vm.rfiForm.stepTwo.$error, function(error) {
      angular.forEach(error, function(field) {
        field.$setTouched();
      });
    });
    // Prevent user from going to next step if current step is invalid
    if (!vm.rfiForm.stepTwo.$valid) {
      isValid = false;
    }
    <!-- form field -->
    <div class="form-group" ng-class="{ 'has-error': rfi.rfiForm.stepTwo.Parent_Suffix__c.$touched && rfi.rfiForm.stepTwo.Parent_Suffix__c.$invalid }">
    
      <!-- field label -->
      <label class="control-label">Suffix</label>
      <!-- end field label -->
      <!-- field input -->
      <select name="Parent_Suffix__c" class="form-control"
              ng-options="item.value as item.label for item in rfi.contact.Parent_Suffixes"
              ng-model="rfi.contact.Parent_Suffix__c" />
      <!-- end field input -->
      <!-- field help -->
      <span class="help-block" ng-messages="rfi.rfiForm.stepTwo.Parent_Suffix__c.$error" ng-show="rfi.rfiForm.stepTwo.Parent_Suffix__c.$touched">
        <span ng-message="required">this field is required</span>
      </span>  
      <!-- end field help -->
    </div>
    <!-- end form field -->

    0 讨论(0)
  • 2020-12-08 02:19

    Note: I know this is a hack, but it was useful for Angular 1.2 and earlier that didn't provide a simple mechanism.

    The validation kicks in on the change event, so some things like changing the values programmatically won't trigger it. But triggering the change event will trigger the validation. For example, with jQuery:

    $('#formField1, #formField2').trigger('change');
    
    0 讨论(0)
  • 2020-12-08 02:20

    I know, it's a tad bit too late to answer, but all you need to do is, force all forms dirty. Take a look at the following snippet:

    angular.forEach($scope.myForm.$error.required, function(field) {
        field.$setDirty();
    });
    

    and then you can check if your form is valid using:

    if($scope.myForm.$valid) {
        //Do something
    }   
    

    and finally, I guess, you would want to change your route if everything looks good:

    $location.path('/somePath');
    

    Edit: form won't register itself on the scope until submit event is trigger. Just use ng-submit directive to call a function, and wrap the above in that function, and it should work.

    0 讨论(0)
提交回复
热议问题