{"id":1818,"date":"2022-08-30T15:19:42","date_gmt":"2022-08-30T15:19:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/custom-directive-isolated-scope-and-form-validation-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:42","modified_gmt":"2022-08-30T15:19:42","slug":"custom-directive-isolated-scope-and-form-validation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/custom-directive-isolated-scope-and-form-validation-collection-of-common-programming-errors\/","title":{"rendered":"Custom Directive ,isolated scope and form validation-Collection of common programming errors"},"content":{"rendered":"<p>I need to create the following component<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>i have defined the form and form validation for it<\/p>\n<pre><code>invalid BP\n<\/code><\/pre>\n<p>i have also created a directive &#8220;bp&#8221; for validating when user enters in the text box<\/p>\n<pre><code>mobileApp.directive(\"bp\", ['orderData', function (orderData) {\nreturn {\nrequire: 'ngModel',\n    link: function (scope, element, attrs, ctrl) {\n        \/\/ctrl.$parsers.unshift(function(viewValue) {\n        element.bind(\"change\", function () {\n            \/\/  element.attr('readonly', true);\n\n            \/\/a promise is returned from the service ajax call\n\n\n            \/\/tell angular to apply changes after the promise is returned \n\n            scope.$apply(function () {\n                console.log(scope);\n                var promise = orderData.vaidateBP(element.val());\n                promise.then(\n                    function (data) {\n                        if (data.length &gt; 0) {\n                            if (scope.SoHeader) {\n                                scope.SoHeader.salesOffice = data[0].soff;\n                                scope.SoHeader.salesOrderType = data[0].otype;\n                                scope.SoHeader.address = data[0].Address;\n                            }\n                            ctrl.$setValidity('bp', true);\n\n                            \/\/  return viewValue;\n                        }\n                        else {\n                            ctrl.$setValidity('bp', false);\n                            \/\/return undefined;\n                        }\n\n                    },\n                        function (response) {\n                            console.log(response);\n                        }\n\n\n                    )\n            });\n\n\n        })\n    }\n\n   }\n}]);\n<\/code><\/pre>\n<p>this works fine ,but how should i go on with the &#8220;lookup button &#8220;!<\/p>\n<p>the user should be allowed to click on the button a dialog will open he can select a value from the list and that value should be filled in the text box . i tried using angularUI&#8217;s dialog ,works like charm but how can i set the validation for the text box to valid after the user selects some thing from the dialog opened by the button,cause user puts some invalid value and then click&#8217;s the look up button to select valid value the text box is still shown as invalid. how should i go on with creating a reusable directive which comprises of this![look-up button][1] and has isolated scope but can set the validation after the user&#8217;s changes in the input are valid as-well-as when he selects a value from the dialog list provided on click of the lookup button<\/p>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:41:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I need to create the following component i have defined the form and form validation for it invalid BP i have also created a directive &#8220;bp&#8221; for validating when user enters in the text box mobileApp.directive(&#8220;bp&#8221;, [&#8216;orderData&#8217;, function (orderData) { return { require: &#8216;ngModel&#8217;, link: function (scope, element, attrs, ctrl) { \/\/ctrl.$parsers.unshift(function(viewValue) { element.bind(&#8220;change&#8221;, function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1818","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1818"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1818\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}