I'm trying to Submit the form to another mocked API endpoint which returns true or false depending on whether the answer provided was correct. I'm new with AngularJS, I'm trying to get it bit by bit.
- How can I display the required message properly from my json file?
- How can I display the answers options and validate them?
HTML:
<my-form ng-app="CreateApp" ng-controller="mainController">
<form ng-submit="submitForm()" novalidate>
<fieldset>
<div ng-repeat="field in result.fields">
<label for="{{field.type}}">{{field.label}}</label>
<input type="{{field.type}}" ngRequired="{{field.required}}">
<span ng-show="{{field.errorMessages}}"></span>
</div>
<!-- not sure how to display the answers options and validate them -->
<input type="{{answer.type}}" name="{{answer.label}}" ngRequired="{{answer.required}}" />
</fieldset>
<button type="button" ng-click="onValidate(); return false;"> Validate</button>
<button type="submit"> Submit </button>
</form>
</my-form>
JS:
var myApp=angular.module('CreateApp', []);
myApp.controller('mainController', function($scope, $http) {
$http.get('form.json').success(function(response) {
$scope.result = response;
console.log($scope.fields);
});
// function to submit the form after all validation has occurred
$scope.submitForm = function() {
// check to make sure the form is completely valid
if ($scope.userForm.$valid) {
alert('our form is amazing');
}
};
});