I need to make sure that users puts a message in a textarea box before they submit the form with ng-click.
Here is the function that is going to run when ng-click is clicked:
$scope.reject = function() {
$http.post( 'http://server.com/api/reject/' + carID, { Message: Message } ).
success(function(data) {
$scope.output = data;
})
}
Here is my HTML:
<div id="reject" class="animated">
<h3>WARNING</h3>
<p>Your are about to <strong><u>REJECT</u></strong> this CAR. Are you sure this is the action that you want to take?</p>
<form name="reject">
<textarea placeholder="Message" ng-model="Message" ng-required="true"></textarea>
<button ng-click="reject.$valid && reject()">Reject</button>
</form>
</div>
My HTML ng-click code it got from this SO post:
<form name="myform">
<input type="text" ng-model='name' ng-required="true" />
<button ng-click="myform.$valid && preview()">Preview</button>
<button ng-click="myform.$valid && update()">Update</button>
</form>
But for some reason this doesn't work for me. If I remove forward.$valid && and just have it as ng-click="reject() it runs just fine although I can still run it without putting in a message. I need the textarea to be required. So I need to validate before the function runs. The snippet I got off the other post should work but I don't know why it isnt.
EDIT - Here is a fiddle: My Fiddle
Now the message box is mine and the other one below that is someone else's working fiddle. I don't see why mine isn't working.
Can someone tell me what i am doing wrong?
reject.$validbe false because the form is not valid if the <textarea> is not populated. It would be invalid because it isng-required. Therefore, the function will not run.