This is a typical example of the use of ng-messages in AngularJS (1.x):
<form name="demoForm">
<input name="amount" type="number" ng-model="amount" max="100" required>
<div ng-messages="demoForm.amount.$error">
<div ng-message="required">This field is required</div>
</div>
<button type="submit">test submit</button>
</form>
see: http://jsfiddle.net/11en8swy/3/
I now want to change this example so the "This field is required" error only shows when the field is touched ($touched
) or the user hits the submit button.
I cannot use the ng-submitted
class on the form since the validation error prevents the submitting of the form.
How should I do this?
Thanks
You can do this using
ng-show
:And use a custom directive. See a working demo:
The directive is basically setting another
hasFocusFoo
field on the ngModel controller then we can easily use that directive.Ah, at the PC at last.
https://plnkr.co/edit/EX3UmoAOKmTKlameBXRa?p=preview
Here is how I handle this task in my solution. form.$setPristine() - sets the field in a
pristine
state, so field isn't$dirty
and error hidden. But after submit I manually state required fields in a$dirty
state, so errors become visible. + if you type something, and delete it after, the error would be visible without submitting a form.