1

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.

1
  • you need write your own valid fro this case... Commented Sep 8, 2015 at 7:16

2 Answers 2

2

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;"/>
Sign up to request clarification or add additional context in comments.

Comments

1

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

I guess this is better than nothing, though I was hoping to leverage the ng-required attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.