0

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?

PLUNKER

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');
            }

        };

});
DDfrontenderLover
  • 470
  • 1
  • 6
  • 23

2 Answers2

0

You can use ngMessages to validate the form.

https://docs.angularjs.org/api/ngMessages/directive/ngMessages

0

Just need to make a few updates to the template to get your desired result.

  1. You need to loop over the options and print out a new input for each one.
  2. You need to use a unique name field for all of the controls so that they can be added to the form correctly.
  3. The ng-show directive expects a boolean so you can convert the existence of the message to a boolean and then inside the span display the text you want to show when the expression is true.

The inner ng-repeat is used to loop over the options and create a new field set for each one. if there are no options available then we use a single list that only contains the field which allows us to access its label and value information just as we do for each of the options.

View:

<my-form ng-app="CreateApp" ng-controller="mainController">    
    <form ng-submit="submitForm()">
        <fieldset>
            <div ng-repeat="(key, field) in result.fields">
              <div ng-repeat="option in ((field.type && field.options) || [field])">
              <label for="{{field.type}}">{{option.label}}</label>
              <input type="{{field.type}}" name="{{key}}" ngRequired="{{field.required}} value="{{option.value}}">
              <span ng-show="{{!!field.errorMessages.required}}">{{field.errorMessages.required}}</span>
              <span ng-show="{{!!field.errorMessages.invalid}}">{{field.errorMessages.invalid}}</span>
            </div>
            </div>
        </fieldset>

        <button type="button" ng-click="onValidate(); return;"> Validate</button>
        <button type="submit"> Submit </button>
    </form>     
</my-form>

Plunkr

Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
  • Hi Teddy, thanks for this, makes sense! how can I validate the whole thing? – DDfrontenderLover Jan 16 '17 at 16:29
  • As in validate the whole form? Also is validation happening server side or client side? – Teddy Sterne Jan 16 '17 at 16:29
  • basically I need to submit the form to another mocked API endpoint which returns true or false depending on whether the answer provided was correct, and display this response in the UI!! no flipping idea how! – DDfrontenderLover Jan 16 '17 at 16:34
  • would you be able to check this? http://plnkr.co/edit/fK6NL8G6zgaTp7d4zM4P?p=preview – DDfrontenderLover Jan 16 '17 at 17:09
  • The issue you are seeing is that the form control names are not unique. See this question: http://stackoverflow.com/questions/23856209/dynamically-naming-input-controls-in-angularjs which talks about ways to fix this – Teddy Sterne Jan 16 '17 at 20:15