According to my form.json file, I have a form that needs validation and a simulated submission.
- Firstly, I need to address this issue:
fnPtr is not a function
- Next, I want to submit the form to a mocked API endpoint that will return true or false. Can I utilize https://docs.angularjs.org/api/ng/service/$http for this purpose? If so, how can I achieve it?
When submitting the form, I need to send an AJAX request which will gather the form data, send it to the specified endpoint, and receive some data in return.
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);
});
$http.get('form.json').success(function(response) {
$scope.result = response;
var fields = response.fields;
$scope.richestClub = fields.answer.options[0].value;
console.log($scope.richestClub);
console.log($scope.fields);
});
});
HTML
<my-form ng-app="CreateApp" ng-controller="mainController">
<form ng-submit="userForm()" name="userForm" novalidate>
<fieldset>
<div ng-repeat="field in result.fields">
<label for="{{field.type}}">{{field.label}}</label>
<input ng-if="field.type != 'radio'"
name="{{field.name}}"
ng-required="{{field.required}}"
value="{{options.value}}"
type="{{field.type}}" />
<div ng-if="field.type == 'radio'">
<div ng-repeat="option in field.options">
<input type="{{field.type}}"
name="{{field.name}}"
ng-required="{{field.required}}"
ng-model="richestClub"
value="{{option.value}}" />{{option.label}}
</div>
</div>
<form-error ng-show="{{!!field.errorMessages.required}}">{{field.errorMessages.required}}</form-error>
<form-error ng-show="{{!!field.errorMessages.invalid}}">{{field.errorMessages.invalid}}</form-error>
</div>
</fieldset>
<button type="submit"
ng-disabled="userForm.$invalid"
ng-click="onSubmit(userForm)"> Submit </button>
</form>
</my-form>