0

Question: How can I show the validation error using only the ng-model if I cannot name the form and its elements.

I have a html form to collect credit card details. To prevent the credit card data from touching my server, I cannot name the form elements. So my form looks like:

<form ng-submit="vm.processForm()">

  <div class="form-row">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number" ng-model="vm.number" required>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YY)</span>
      <input type="text" size="2" data-stripe="exp_month" ng-model="vm.exp_month" required>
    </label>
    <span> / </span>
    <input type="text" size="2" data-stripe="exp_year" ng-model="vm.exp_year" required>
  </div>

  <div class="form-row">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc" ng-model="vm.cvc" required>
    </label>
  </div>

  <input type="submit" class="submit" value="Submit Payment">
</form>

Usually in Angular, I used to check validation using the form elements name, for example like this:

<p ng-show="userForm.creditcard.$error.required">Your credit card number is required.</p>

But since I cannot name the form and its elements, how can I show the validation error using only the ng-model? Because, the following doesn't work:

<p ng-show="vm.number.$error.required">Your credit card number is required.</p>

I am using Angular v1.4.8.

10
  • 1
    <p ng-show="form.$submitted && !vm.number">Blabla</p> Commented Oct 27, 2016 at 19:27
  • Oh yeah, good idea. Can I use a similar way for maxlength and minlength validation? Commented Oct 27, 2016 at 19:32
  • 1
    Well, sure. You can check if vm.number.length > x. However this is wrong. Why can't you use names and validate the way it's meant to be? Commented Oct 27, 2016 at 19:33
  • vm.number.length should work. Simple, but it did not strike me. The reason I can't use names because adding name would make the user-supplied data in those fields to be passed to my server when the form is submitted. I dont want that since not naming means I no longer need to worry about redacting logs, encrypting cardholder details, or other burdens of PCI compliance. Commented Oct 27, 2016 at 19:36
  • 1
    ng-submit doesn't send anything anywhere. It just calls the function you put in it. The form data is in the ng-model attributes you have in each input. I don't see how names are a problem. Anyway.... Commented Oct 27, 2016 at 19:50

1 Answer 1

2

I created a directive to export de the model controller. I don't think that is the best way but it works.

.directive('exportModel', function () {
    return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attr, ngModel) {
        attr.$observe('exportModel', function (value) {
        scope[value] = ngModel;
      })
    }
  }
})

http://jsfiddle.net/Lvc0u55v/11352/

Sign up to request clarification or add additional context in comments.

Comments

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.