Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Establezca programáticamente todos los campos de formulario en ng-touched en el envío del formulario

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 is required </span> </div> <input type="submit" ng-click="submit()" value="Submit" class="btn btn-default">

Estoy tratando de hacer que mi clase 'has-error' se active para campos no válidos cuando un usuario hace clic en enviar.

Pensaría que podrías hacer algo como esto:

 $scope.submit = function () { if ($scope.form.$invalid) { angular.forEach($scope.form.$invalid, function(field) { field.$setTouched(); }); alert("Form is invalid."); } };

Pero no hay un método $setTouched en https://docs.angularjs.org/api/ng/type/form.FormController

EDITAR: tenga en cuenta que $setTouched existe, está en https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

if ($scope.form.$invalid) { angular.forEach($scope.form.$error, function (field) { angular.forEach(field, function(errorField){ errorField.$setTouched(); }) }); alert("Form is invalid."); }

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

over 4 years ago · Santiago Trujillo Report

0

Pruebe el $ enviado recientemente agregado

 <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 || form.$submitted"> First Name is required

over 4 years ago · Santiago Trujillo Report

0

Ampliando la respuesta de Alexey, puede agregar un nuevo método a FormController que hará lo mismo, por lo que no es necesario copiar el código de una función de envío a otra:

 // config.js export default function config($provide) { $provide.decorator('formDirective', $delegate => { const fn = $delegate[0].controller.prototype if (!('$setTouched' in fn)) fn.$setTouched = function() { if (this.$invalid) { Object.values(this.$error).forEach(controls => { controls.forEach(control => control.$setTouched()) }) } } return $delegate }) }
 // controller.js $scope.submit = function () { if ($scope.form.$invalid) { $scope.form.$setTouched(); alert("Form is invalid."); } };

En caso de que alguien se pregunte por qué alguien querría hacer este tipo de validación: falta la validación de restricciones de iOS, por lo que se llama a ng-submit incluso en formularios no válidos.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!