Nicolas Laplante
unread,Aug 8, 2012, 11:41:13 AM8/8/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ang...@googlegroups.com
Let's say my form has 2 radio buttons, "YES" and "NO".
I have 2 text fields which are hidden by default, unless I click on the "NO" radio.
I've made a directive which adds the 'required="required"' attribute to the element when an expression evaluates to true:
myModule.directive("requiredIf", function () {
return {
restrict: "A",
link: function (scope, element, attr, ctrl) {
scope.$watch(attr.requiredIf, function (value) {
if (value === true) {
element.attr("required", "required");
}
else {
element.removeAttr("required");
}
});
}
};
});
The way I've put my fields is as follow:
<input type="radio" name="choice" value="Y" ng-model="choice" required>
<input type="radio" name="choice" value="N" ng-model="choice" required>
<input class="input-small" type="number" name="price" ng-model="price" required-if="choice == 'N'">
Using Firebug console, I can confirm that the "required='required'" attribute is added to my text field. However, if I leave the field blank, the $invalid property in the form controller stays false, as if the controller didn't know that this field is now required. Is there a way to notify the form controller that validation rules changed on one or more fields within it after initialization?