'Angular+Bootstrap input validation error class
I'm trying to make example from this article using angular+bootstrap. I have this working code:
<div ng-controller="Ctrl">
<br>
<hr>
<form name="signup_form" novalidate ng-submit="signupForm()">
<fieldset>
{{' signup_form.submitted \''+signup_form.submitted+ '\' ' }}<br>
{{' submitted \''+submitted+ '\' ' }}<br>
{{' $scope.signup_form.$valid \''+signup_form.$valid+ '\'' }}
<div class="control-group" ng-class="{error: signup_form.name.$dirty && signup_form.name.$invalid}">
<legend>Signup</legend>
<div class="row">
<div class="span12">
<label>Your name</label>
<input type="text"
placeholder="Name"
name="name"
ng-model="signup.name"
ng-minlength=3
ng-maxlength=20 required/>
</div>
</div>
</div>
<button type="submit" class="button radius">Submit</button>
</fieldset>
</form>
</div>
The questions are:
This piece of code:
<div class="control-group" ng-class="{error: signup_form.name.$dirty && signup_form.name.$invalid}">does not look very nice. I feel it's not good practice to do it this way. How could it be more easy and correct?
If I press input field, I instantly got red borders. I found this problem github.com/twbs/bootstrap/issues/1675, but it is a very old thread. What's the best and correct way to avoid this red box on empty field click?
Any overall code suggestions are welcome. ty! )
Solution 1:[1]
I've done things like this in forms I've created:
<div class="form-group col-md-3 input-group-lg" ng-class="{true: 'has-error',false: 'is-required'}[createContacts.proposer.$dirty && createContacts.proposer.$invalid]">
<label class="control-label" for="proposer">Name *</label>
<input type="text" class="form-control" name="proposer" id="proposer" placeholder="First & Last Name" ng-model="proposer.name" required>
<span class="help-block" ng-show="createContacts.proposer.$dirty && createContacts.proposer.$error.required">Proposer's name is required.</span>
</div>
ng-class="{true: 'has-error',false: 'is-required'}[createContacts.proposer.$dirty && createContacts.proposer.$invalid]"
is-required class is not a part of bootstrap, its a class I created to color code required objects when not in error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
