I want an error message to appear if the user clicks the submit button without content, and I want the submit button to be disabled. I can get either one, but not both at the same time.
The code below triggers a message, but resolves an empty todo element.
<form name="todoForm" novalidate > <div ng-messages="todoForm.new.$error" ng-if="todoForm.$submitted"><div ng-message="required">Add Your Item Below...</div></div> <input type="text" name="new" placeholder="start typing..." autofocus data-ng-model="newTodo" required=""/> <button input type="submit" ng-click="addTodo()" >Add To List</button> </form>
This version disables the submit button but does not display a message
<form name="todoForm" novalidate > <div ng-messages="todoForm.new.$error" ng-if="todoForm.$submitted"><div ng-message="required">Add Your Item Below...</div></div> <input type="text" name="new" placeholder="start typing..." autofocus data-ng-model="newTodo" required=""/> <button input type="submit" ng-click="addTodo()" data-ng-disabled="todoForm.$invalid" >Add To List</button> </form>
I assume this is because the message cannot be displayed when the enter button is disabled because nothing has been sent?
I tried using $ disabled and $ invalid, but they did not work.
angularjs forms message disabled-input
vtechmonkey
source share