1

I would like to dynamically display Person and Address data using label and input value in Summary Section. As the user edits the form fields, a list items with label + value should display in the summary tables. If value has been removed in the form, that associated label and value should be removed from the Summary Section.

I have added client side validation for each input element. I tried to solve this and couldn't figure out what is best way to do it. Any help would be appreciated.

Example:

// the main (app) module
var myApp = angular.module("myApp", []);

// add a controller
myApp.controller("myCtrl", function($scope) {
  $scope.vm = {
    caller: {
      person: {
        firstName: '',
        lastName: '',
        phoneOne: '',
        email: ''
      },
      address: {
        lineOne: '',
        lineTwo: ''
      }
    }
  };
  $scope.save = function() {
    console.log($scope.vm);
  }
});

// add a directive
myApp.directive('showErrors', function($timeout, $compile) {
  return {
    restrict: 'A',
    require: '^form',
    link: function(scope, el, attrs, formCtrl) {
      // find the text box element, which has the 'name' attribute
      var inputEl = el[0].querySelector("[name]");
      // convert the native text box element to an angular element
      var inputNgEl = angular.element(inputEl);
      // get the name on the text box
      var inputName = inputNgEl.attr('name');
      // only apply the has-error class after the user leaves the text box
      var blurred = false;

      inputNgEl.bind('blur', function() {
        blurred = true;
        el.toggleClass('has-error', formCtrl[inputName].$invalid);
      });

      scope.$watch(function(scope) {
        return formCtrl[inputName].$invalid;
      }, function(invalid, scope) {
        // we only want to toggle the has-error class after the blur
        // event or if the control becomes valid
        if (!blurred && invalid) {
          return
        }
        el.toggleClass('has-error', invalid);
      });

      scope.$on('show-errors-check-validity', function() {
        el.toggleClass('has-error', formCtrl[inputName].$invalid);
      });

      scope.$on('show-errors-reset', function() {
        $timeout(function() {
          el.removeClass('has-error');
        }, 0, false);
      });
    }
  }
});
.form-group .help-block {
  display: none;
}
.form-group.has-error .help-block {
  display: inline;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
  <form name="claimForm" ng-submit="save()">
    <h3>PERSON</h3>
    <div class="col-md-6">
      <div class="form-group form-caller" show-errors>
        <label class="control-label">First Name<span class="help-block" ng-if="claimForm.callerFirstName.$error.required"><i>[required]</i></span>
        </label>
        <input type="text" name="callerFirstName" ng-model="vm.caller.person.firstName" class="form-control" required="" />
      </div>
    </div>
    <div class="col-md-6">
      <div class="form-group form-caller" show-errors>
        <label class="control-label">Last Name<span class="help-block" ng-if="claimForm.callerLastName.$error.required"><i>[required]</i></span>
        </label>
        <input type="text" name="callerLastName" ng-model="vm.caller.person.lastName" class="form-control" required="" />
      </div>
    </div>
    <hr />
    <h3>ADDRESS</h3>
    <div class="col-md-6">
      <div class="form-group" show-errors>
        <label class="control-label">Address Line 1<span class="help-block" ng-if="claimForm.addressOne.$error.required"><i>[required]</i></span>
        </label>
        <input type="text" name="addressOne" ng-model="vm.caller.address.lineOne" class="form-control" required="" />
      </div>
    </div>
    <div class="col-md-6">
    <div class="form-group" show-errors>
      <label class="control-label">Address Line 2<span class="help-block" ng-if="claimForm.addressTwo.$error.required"><i>[required]</i></span>
      </label>
      <input type="text" name="addressTwo" ng-model="vm.caller.address.lineTwo" class="form-control" required="" />
    </div>
     </div>
    <hr />
    <input type="submit" id="submit" value="SUBMIT" class="btn btn-primary btn-lg" />
    {{vm | json }}
  </form>
  <h2>Summary</h2>
  <div id="person">
    <h3>PERSON </h3>
  </div>
  <hr />
  <div id="address">
    <h3>ADDRESS</h3>
  </div>
</body>

Thanks in Advance

5
  • Fiddler URL : jsfiddle.net/pakpatel/0rttw2wq/4 Commented Aug 17, 2016 at 3:33
  • check the firstname & lastname in summary section here: jsfiddle.net/ta3e69L0 Commented Aug 17, 2016 at 17:04
  • I see you need even label to be printed, I believe in that case your label and value both needs to be bind with the field. Ref. stackoverflow.com/questions/29700715/… Commented Aug 17, 2016 at 17:11
  • @seUser I like this idea but still i think this could be done better way. Commented Aug 17, 2016 at 17:43
  • @seUser this is what i came up with using above linked you shared jsfiddle.net/pakpatel/51hzc753/9 but i still hate that i have to bring each field as data object. Commented Aug 17, 2016 at 18:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.