Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Not sure if this is possible but I'm trying, and keep coming up short.

http://plnkr.co/edit/Gcvm0X?p=info

I want a 'E' (element) directive that is replaced with a more complex nested HTML node using the 'templateUrl' feature of directives.

HTML defining the directive (form tag included for complete mental image):

<form id="frm" name="frm">

      <ds-frm-input-container
        class="col-md-1"
        frm-Name="frm"
        frm-obj="frm"
        input-name="txtFName"
        ds-model="user.firstName"></ds-frm-input-container>  

</form>

TemplateUrl contents which 'replaces' the above directive 'ds-frm-input-container' HTML element:

<div>

  <input
    required
    ng-minlength=0
    ng-maxlength=50
    class="form-control"
    ng-model="dsModel" 
    placeholder="{{dsPlaceHolder}}" />  

  <span ng-if="showErrs" class="label label-danger">FFFFF: {{dsModel}}</span>

</div>

Controller and Directive:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = "Nacho";
  $scope.user = {};
  $scope.user.firstName = ""; 


})
.directive('dsFrmInputContainer', function(){

    var ddo = { 
      priority: 0,
      restrict: 'AE',
      scope: 
      {
        frmName: '@',
        inputName: '@',
        dsPlaceHolder: '@',
        dsModel: '=',
        frmObj: '='
      },
      templateUrl: 'template1.html',
      replace: true,
      controller: function($scope)
      {
        $scope.showErrs = true;

      },
      compile: function compile(ele, attr) {
        return {
          pre: function preLink(scope, ele, attr, controller) 
          { 

          },
          post: function postLink(scope, ele, attr, controller) 
          { 
            var txt = ele.find('input');

            txt.attr('id', scope.inputName);
            txt.attr('name', scope.inputName);

            //BLUR
            txt.bind('blur', function () {

              console.log("BLUR BLUR BLUR");

              angular.forEach(scope.frmObj.$error, function(value, key){
                  var type = scope.frmObj.$error[key];

                  for(var x=0; x < type.length; x++){
                    console.log(type[x]);
                  }
              });        

              event.stopPropagation();
              event.preventDefault();

            });              
          }
        };
      },

    };  

    return ddo;

});

The directive replaces just fine and the input element is named just fine. The form object however doesn't include the input element name in the error information. This makes it impossible for me to single out the input element during a 'blur' event that is setup in the directive.

I am doing this trying to reduce the show/hide logic 'noise' in the html for error messages (spans) and it should be reusable.


UPDATE (2014.01.28):


2014.01.28: Added promises. There is a service that allows validation on button clicks. NOT USING built in angular validation anymore found some compatibility issues with another library (or viceversa).

ORIGINAL: Here is my form validation directive vision completed (plnkr link below). Completed in concert with the help of the stack overflow community. It may not be perfect but neither are butterfingers but they taste good.

http://plnkr.co/edit/bek8WR?p=info

share|improve this question
    
several major issues....isolated scope, changing element in link without using $compile. Read ng-model docs section pertaining to isolated scope. Should be able to do all this without isolated scope –  charlietfl Dec 2 '13 at 23:53
    
@charlietfl I understand isolated scope and I've tried the alternative. I'm not sure we can say isolated scope is an issue here. It may not be considered proper usage, but I'm not sure it's an issue here. $compile however could be the issue but my current understanding would have me believing I am using $compile via the compile pre (tried here 1st) & post; post being the equivalent of 'link' so I've read. If I'm not understanding this correctly please let me know. I will try adjusting scope and try learning how to use $compile. Thanks. –  Hash Sling Slasher Dec 3 '13 at 3:12
    
did you read ng-model docs? –  charlietfl Dec 3 '13 at 3:21
    
this one @charlietfl: link –  Hash Sling Slasher Dec 3 '13 at 3:33
    
@charlietfl fyi: updated –  Hash Sling Slasher Jan 28 at 14:55
add comment

2 Answers

up vote 1 down vote accepted

So here is a link that has the name variables set as expected on the given input form error object. http://plnkr.co/edit/MruulPncY8Nja1BUfohp?p=preview

The only difference is that the inputName is read from the attrs object and is not part of the scope. This is then read before the link function is returned, in the compile phase, to set the template DOM correctly.

share|improve this answer
    
Thanks for your time. –  Hash Sling Slasher Dec 3 '13 at 13:22
    
FYI:I updated code in my example above. –  Hash Sling Slasher Jan 28 at 14:56
add comment

I have just spent quite a while trying to sort this problem out, and while this is not exactly what you were looking for, his is my attempt. It uses bootstrap for all the styling, and allows for required and blur validation, but its definitely not finished yet. Any thoughts or advice much appreciated.

https://github.com/mylescc/angular-super-input

share|improve this answer
    
Here is a plunker I used as a prototype for updating my code to start using promises as I found they are a wonderful pattern for working with async (custom rules). I opted to stop using the built in Angularjs validation after finding it didn't play nice (or viceversa) with a masked edit library I was using. Really checking for all that stuff is basic and straight forward. plnkr.co/edit/QCvt3dugTDip3cmyP74r?p=preview –  Hash Sling Slasher Jan 28 at 14:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.