Reputation: 4361
I have a form with some input type text, input type number, checkbox, radio, a select option and a submit button. I want to validate each input. If a required input is not filled, I want to display a message.
Here's what happened : For my first input, the message appears when I did not select a radio and even when I select.
<div class="form-group has-feedback">
I am : <br>
<label class="radio-inline">
<input type="radio" ng-model="sex" name="sex" id="sex" value="femme"> <strong>Woman</strong>
</label>
<label class="radio-inline">
<input type="radio" ng-model="sex" name="sex" id="sex" value="homme"> <strong>Man</strong>
</label>
<div ng-show="form.sex.$error.required">
<p class="help-block">
Sex is required
</p>
</div>
</div>
I miss something it the same for others inputs. I don't see what I am missing ?
Second, I want to disable the submit button when there is an error on a input. This does not work. Here is my code :
<button ng-click="sendMessage();" class="btn btn-success pull-center" ng-disabled="form.$invalid" type="submit">Let me know</button>
You can see the complete code on Plunker. Thanks for helping
Upvotes: 1
Views: 1660
Reputation: 1885
You had 2 problems, first the name of your application module was wrong,
("owmuchApp" in the script.js
and "myApp" in the index.html
) so the application wasn't even loading.
You need set the ng-required field of the radio buttons group this way:
<input type="radio" ng-model="sex" ng-required="!sex" name="sex" id="sex" value="femme"> Woman
<input type="radio" ng-model="sex" ng-required="!sex" name="sex" id="sex" value="homme"> Man
Here is the working solution
Update
I forget to mention that i added a new condition on the show message:
<div ng-show="form.sex.$dirty && form.sex.$invalid">
<p ng-show="form.sex.$error.required" class="help-block">
Sex is required
</p>
</div>
Will be shown only when the user tries to submit the form, otherwise the message was been shown soon as the form was rendered.
Upvotes: 2
Reputation: 2366
Here is the updated plunker http://plnkr.co/edit/ZfUE3uEjklJ2pow46Gs8?p=preview
<button ng-click="sendMessage();" class="btn btn-success pull-center" ng-disabled="!myForm.$valid" type="submit">Let me know</button>
Upvotes: 2