Angular.js $setValidity onload Page

Asked

Viewed 292 times

2

  • Setting:

    I have a form with 3 steps, a jsp for each step. In a given jsp there is a input text with the directive ng-minlength and a onChange calling a function that validates with the $setValidity if the given value is invalid.

  • Problem:

    When the field is invalid and the navigation occurs between the steps, when returning to the stage the field loses the invalid status. As a solution, I tried to validate the field when loading the page but error occurs when executing the code below because the form has not been loaded yet:

    $scope.form.numeroCartao.$setValidity("valid", true);

  • Question:

    How to validate a field with the $setValidity when loading the page?

1 answer

2

Instead of setting the field value using the value input, load the value in ng-model.

Instead of doing this:

<input type="text" ng-minlenth="10" ng-model="teste" value="${ vdo.teste }"/>

Do it:

<form ng-init="teste = '${ vdo.teste }';">
...
<input type="text" ng-minlenth="10" ng-model="teste"/>

Another... don’t listen to the native event onChange element, with Angular the ideal is to use the directive on-change, or create a $watch on your controller.

  • Hello Victor, I started calling my function in ng-init right in the form, but it still can’t locate the name of the form I want to invalidate according to the code: $scope.form.numeroCartao.$setValidity("valid", false);.

  • TypeError: Não é possível obter a propriedade 'numeroCartao' de referência indefinida ou nula

  • The card number has not yet been loaded, inside the form.

  • Dude, put your HTML in a Gist or Jsfiddle so we have a better idea of what you’re doing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.