Validate form control based on other controls, access other control in validate function

后端 未结 1 1753
失恋的感觉
失恋的感觉 2021-01-21 15:54

I would like to validate email and phone based on the following:

If both email and phone are empty or the value containing them are not valid then both are invalid with

相关标签:
1条回答
  • 2021-01-21 16:36

    If you need access to the form in your directive, you need to require it:

    require: ['^form', 'ngModel'],
    link: function($scope, elem, attrs, ctrls) {
       var form = ctrls[0];
       var ngModel = ctrls[1];
    
       if(form) {
         // use form to get whatever it is you care about.
         var whatever = form[otherProperty].$error.something;
       }
    }
    

    So given the above, you should be able to pass the form controller into your custom validation methods as a third argument, and handle whatever you need.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题