I scribbled this jsFiddle to describe the problem at hand. Basically I have a form with certain ng-required text inputs. The catch is that the user cannot type inside them, but has to click on a button which opens up a dialog with a list of allowed values; clicking on one sets the input's ng-model accordingly (in my example, the button next to the input sets the ng-model right away, but you get the jist).
Trouble is that if I set ng-disabled (comprehensible) or ng-readonly (this honestly I can't figure out why) the form is submitted even if the field is left empty, whereas I'd like to enforce user input. How could I achieve this? Cheers.
-
you need write your own valid fro this case...Daredzik– Daredzik2015-09-08 07:16:09 +00:00Commented Sep 8, 2015 at 7:16
Add a comment
|
2 Answers
I got around this by adding another input sharing the same model and putting it right below the visible one. You can't just do a hidden input however (either by setting type="hidden" or ng-hide and whatnot), as hiding it entirely won't let the validation show up.
<!-- Normal input: we want this to be required, yet read-only -->
<input id="inputMyValue" ng-model="myValue" ng-click="openSelectionDialog()" ng-readonly="true"/>
<!-- Hidden input: not really hidden - just visible enough for the validation to work -->
<input id="inputMyValueHidden" ng-model="myValue" ng-required="true" style="height: 0; border: 0;"/>
Comments
Write your own validation function on submit:
$scope.validateForm = function() {
if ($scope.myDisabledInput === '') {
return;
}
};
I would alert the user somehow, but this should at least point you in the right direction.
1 Comment
Andrea Aloi
I guess this is better than nothing, though I was hoping to leverage the
ng-required attribute