I have a form made with Bootstrap 4 and AngularJS and I want to make a simple form validation where I could:
- validate input fields AFTER user has touched them,
- show two different error messages depending on validation constraint,
- create custom validation constraint (e.g. compare password and password confirmation field).
1.
In previous versions of Bootstrap you could do something like:
<div class="form-group" ng-class="{'has-error': userForm.email.$invalid && userForm.email.$touched}">
but with Bootstrap 4, there is no 'has-error' class. I tried to use 'is-invalid' class instead
<div class="form-group" ng-class="{'is-invalid': userForm.email.$invalid && userForm.email.$touched}">
but nothing happens. I managed to make it work as it is shown in Bootstrap 4 Documentation but validation happens only after the form submission and not when user has touched the input field.
2.
There are no error messages in Bootstrap 4, only 'invalid-feedback' and I can't figure out how to change message text depending on which constraint was violated. I tried something like:
<div class="invalid-feedback">
<p ng-show="userForm.email.$error.required">Required</p>
<p ng-show="userForm.email.$error.email">Enter a valid email</p>
</div>
but this does not work.
3.
I tried to make an Angular directive but because 1. and 2. didn't work I couldn't test this.
Any help will be appreciated.
UPDATE
As Peter Wilson suggested, nothing was wrong with my code. The code didn't work because my form was in a modal window and modal window must have a separate controller in AngularJS to be able to work correctly.
the
is-invalidclass is the right answer you may have another problem with your form so check my below demoNote to test touched input you have to focus on the input and then leave focus to trigger touched state