javascript - Programmatically set all form fields to ng-touched on form submission -


html:

<div class="form-group"       ng-class="{ 'has-error' : form.firstname.$invalid && form.firstname.$touched }">   <label for="firstname"           class="control-label">          first name   </label>   <input type="text"           name="firstname"           id="firstname"           ng-model="editableuser.firstname"           class="form-control"           required>   <span class="help-block"          ng-show="form.firstname.$error.required && form.firstname.$touched">         first name required   </span> </div>  <input type="submit"         ng-click="submit()"         value="submit"         class="btn btn-default"> 

i'm trying 'has-error' class kick in invalid fields when user clicks submit.

i think this:

$scope.submit = function () {   if ($scope.form.$invalid) {     angular.foreach($scope.form.$invalid, function(field) {       field.$settouched();     });     alert("form invalid.");   } }; 

but there no $settouched method in https://docs.angularjs.org/api/ng/type/form.formcontroller

edit: realize $settouched exist, it's in https://docs.angularjs.org/api/ng/type/ngmodel.ngmodelcontroller

if ($scope.form.$invalid) {     angular.foreach($scope.form.$error, function (field) {         angular.foreach(field, function(errorfield){             errorfield.$settouched();         })     });     alert("form invalid."); } 

plunker: http://plnkr.co/edit/xmvotkiq0yvfukrvsz11


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -