Skip to content

Instantly share code, notes, and snippets.

@molekilla
Last active August 29, 2015 14:06
Show Gist options
  • Save molekilla/979960da22d49c6c8446 to your computer and use it in GitHub Desktop.
Save molekilla/979960da22d49c6c8446 to your computer and use it in GitHub Desktop.
required only after ng blur and ng change
<!-- parcialmente basado en yearofmoo.com -->
<form name="formulario">
<div class="field">
<label for="correo">Correo Electronico:</label>
<input type="email"
name="correo" ng-change="MiCtrl.checkRequired(datos.email)" ng-blur="MiCtrl.checkRequired(datos.email)"
ng-model="datos.email"
/>
<div ng-messages="formulario.correo.$error">
<div ng-message="required">Campo requerido</div>
<div ng-message="email">Debe ser un correo electronico.</div>
</div>
</div>
</form>
<!-- MiCtrl controller -->
...
this.checkRequired = function(modelCtrl) {
var isRequired = (modelCtrl.$viewValue || '').length === 0;
modelCtrl.$error.required = isRequired;
modelCtrl.$setValidity(modelCtrl.$name, !isRequired);
};
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment